Studyon Minte9.com
ZCE 5.3

Study

Creation



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_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 )
  list() - assign variables as if they were array
<?php // Atention: Assigns the values starting with the right-most parameter // Example 1 list($a, $b) = array(1, 2); echo $a; // Output: 1 echo $b; // Output: 2 // Example 2 $a = array(); list($a[0], $a[1], $a[2]) = array(1, 2, 3); print_r($a); // Array ( [2] => 3 [1] => 2 [0] => 1 )
  each() - return current key/value and advance the cursor
<?php // Example 1 $arr = array(1,2,3); $res = each($arr); print_r($res); // Array ( [1] => 1 [value] => 1 [0] => 0 [key] => 0 ) echo $res['value']; // Output: 1 // Example 2 $arr = array('a', 'b', 'c'); while(list($k, $v) = each($arr)) { echo $k . $v; } // 0a 1b 2c
  extract() - assign variable from key/value
<?php $array = array('a'=>1, 'b'=>2, 'c'=>3); extract($array); echo $b; // Output: 2
  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_fill() - Fill an array with values
<?php $arr = array_fill(5, 2, 'a'); // 5 - start index // 2 - range print_r($arr); // Array ( [5] => a [6] => a )
  array_fill_keys() - Fill an array with values, specifing keys
<?php $keys = array('a', 'b', 'c'); $arr = array_fill_keys($keys, 'x'); print_r($arr); // Array ( [a] => x [b] => x [c] => x )