MemoryRefresh!

Array / Create   Create



Questions 1 Answers 0%

Pages   0 from 91
Questions   0 from 142

Reset


range() - Creates an array containing a range of elements
<?php $images = range(1,100); // 1,2,3 ... $images = range(0,100,10); // 10,20,30 ... array_chunk() - split array into chunks
<?php $images = range(0,100); $chunks = array_chunk($images, 2); // 2 - size of each chunk // 1 - default foreach($chunks as $chunk) { // 50 chunks echo "<hr>"; foreach($chunk as $v) { echo $v; } } array_combine() - key from arr1, values from arr2
<?php $keys = array('green', 'yellow'); $values = array('avocado', 'banana'); $arr = array_combine($keys, $values); print_r($arr); // Array ( [green] => avocado [yellow] => banana ) array_merge() - Merge one or more arrays
<?php // Example 1 $one = 'foo'; $two = array('a', 'b'); $result = array_merge($one, $two); // Warning: Argument #1 is not an array $result = array_merge((array)$one, $two); print_r($result); // Array ( [0] => foo [1] => a [2] => b ) // Example 2 (same keys not numeric) // If the input arrays have the same string keys, // then the later value for that key will overwrite the previous one $arr1 = array('color'=> 'red', 1); $arr2 = array('a', 'color'=>'green'); $arr3 = array_merge($arr1, $arr2); print_r($arr3); // Array ( [color] => green [0] => 1 [1] => a ) // Example 3 (same keys numeric) // If, however, the arrays contain numeric keys, // the later value will not overwrite the original value $arr1 = array(1,2); arr2 = array(3,4); $arr3 = array_merge($arr1, $arr2); print_r($arr3); // Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) // Example 4 // If don't want to ovewrite the elements from the first array // use the + array union operator $arr1 = array('color'=> 'red', 1); $arr2 = array('color'=>'green', 2, 3); $arr3 = $arr1 + $arr2; print_r($arr3); // Array ( [color] => red [0] => 1 [1] => 3 ) array_diff() - difference of arrays
<?php // All the values of $a that do not appear in $b // are retained $a = array (1, 2, 3); $b = array (1, 3, 4); print_r(array_diff ($a, $b)); // Array ( [1] => 2 ) array_intersect() - intersect arrays
<?php // Like with array_diff() array_intersect only keeps // in consideration the value of each element $a = array (1, 2, 3); $b = array (1, 3, 4); print_r(array_intersect ($a, $b)); // Array ( [0] => 1 [2] => 3 )

Related Pages

Basics
Operations
Create
Key-value
Search
Modify
Pointer
Sort
Stacks
Randomize
Storage


0% 100%
 
 
0 pages 91 pages
1) How do you create a new array containing 10 numbers?





2) How do you create an array using an array for keys and another for values?





3) How do you append an array to another and not overwrite the keys?








range() - Creates an array containing a range of elements
<?php $images = range(1,100); // 1,2,3 ... $images = range(0,100,10); // 10,20,30 ... array_chunk() - split array into chunks
<?php $images = range(0,100); $chunks = array_chunk($images, 2); // 2 - size of each chunk // 1 - default foreach($chunks as $chunk) { // 50 chunks echo "<hr>"; foreach($chunk as $v) { echo $v; } } array_combine() - key from arr1, values from arr2
<?php $keys = array('green', 'yellow'); $values = array('avocado', 'banana'); $arr = array_combine($keys, $values); print_r($arr); // Array ( [green] => avocado [yellow] => banana ) array_merge() - Merge one or more arrays
<?php // Example 1 $one = 'foo'; $two = array('a', 'b'); $result = array_merge($one, $two); // Warning: Argument #1 is not an array $result = array_merge((array)$one, $two); print_r($result); // Array ( [0] => foo [1] => a [2] => b ) // Example 2 (same keys not numeric) // If the input arrays have the same string keys, // then the later value for that key will overwrite the previous one $arr1 = array('color'=> 'red', 1); $arr2 = array('a', 'color'=>'green'); $arr3 = array_merge($arr1, $arr2); print_r($arr3); // Array ( [color] => green [0] => 1 [1] => a ) // Example 3 (same keys numeric) // If, however, the arrays contain numeric keys, // the later value will not overwrite the original value $arr1 = array(1,2); arr2 = array(3,4); $arr3 = array_merge($arr1, $arr2); print_r($arr3); // Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) // Example 4 // If don't want to ovewrite the elements from the first array // use the + array union operator $arr1 = array('color'=> 'red', 1); $arr2 = array('color'=>'green', 2, 3); $arr3 = $arr1 + $arr2; print_r($arr3); // Array ( [color] => red [0] => 1 [1] => 3 ) array_diff() - difference of arrays
<?php // All the values of $a that do not appear in $b // are retained $a = array (1, 2, 3); $b = array (1, 3, 4); print_r(array_diff ($a, $b)); // Array ( [1] => 2 ) array_intersect() - intersect arrays
<?php // Like with array_diff() array_intersect only keeps // in consideration the value of each element $a = array (1, 2, 3); $b = array (1, 3, 4); print_r(array_intersect ($a, $b)); // Array ( [0] => 1 [2] => 3 )

References









Programming

Php
   
Regex
   
MySQL
   
Css
   
jQuery
   
Git
   


References