Studyon Minte9.com
ZCE 5.3

Study

Modify



array_unique() - Remove duplicate values from an array

<?php $arr = array(1, 2, 'b'=>'green', 2); $arr = array_unique($arr); print_r($arr); // Array ( [0] => 1 [1] => 2 [b] => green )
  array_map() - Same as array_walk (which needs reference)
<?php // Example 1 function cube($n) { return pow($n, 3); } $a = array(1,2,3); $b = array_map('cube', $a); print_r($b); // Array ( [0] => 1 [1] => 8 [2] => 27 ) // Example 2 (more compact) $result = array_map(create_function('$x', 'return pow($x, 3);'), array(1,2,3)); print_r($result); // [ 1, 8, 27 ]
  array_walk() - Apply a user function to every member of an array
<?php $arr = array (1,2,3); array_walk($arr, create_function('&$x', '$x *= 2;')); print_r($arr); // [ 2, 4, 6 ]
  array_pad() - Pad array to the specified length with a value
<?php $array = array(1,2,3); $result = array_pad($array, 6, 0); print_r($result); // [ 1, 2, 3, 0, 0, 0 ] echo str_pad(111, 6, 0); // 111000 // usage example <?php $topPlayers = array("John", "Mary", "Tom"); $topTenPlayers = array_pad($topPlayers, 10, ""); // add 7 empty places
  array_reduce() - Iteratively reduce the array to a single value using a callback function
<?php echo array_reduce(array(1,2,3,4), create_function('$x, $y', 'echo $x; return $x + $y;')); // Output: 1 / 3 / 6 / 10 // $x is the result pass by reference // mixed callback ( mixed &$result , mixed $item )
  array_replace() - Replace elements from passed arrays into the first array
<?php // from PHP 5.3.0 $arr = array('a','b','c'); $arr1 = array(0=>'d'); $arr2 = array(1=>'e'); $result = array_replace($arr, $arr1, $arr2); print_r($result); // [ d, e, c ]
  array_slice() - Extract a slice of the array
<?php $arr = array(1, 2, 3, 4, 5); print_r(array_slice($arr, 2)); // [ 3, 4, 5 ] print_r(array_slice($arr, 0, 2)); // [ 1, 2, 3 ] print_r(array_slice($arr, -2, 1)); // Array ( [0] => 4 ) print_r(array_slice($arr, -2, 1, true)); // Array ( [3] => 4 ) preserve keys
  array_splice() - Remove a portion of array and replace it with something else
<?php $arr = array(1, 2, 3, 4, 5); array_splice($arr, 2); print_r($arr); // [ 1, 2 ] $arr = array(1, 2, 3, 4, 5); array_splice($arr, 2, count($arr), array(10,11)); print_r($arr); // [ 1, 2, 10, 11 ]
  array_flip() - values became keys
<?php $a = array('a', 'b'); $b = array_flip($a); print_r($b); // Array ( [a] => 0 [b] => 1 )
  array_reverse() - Inverts the order of the array's elements
<?php $a = array('a', 'b'); $b = array_reverse($a); print_r($b); // Array ( [0] => b [1] => a )