ExpertRefresh

Array / Modify  

Related Pages

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


0.0% 100%
 
 
0 pages 298 questions
1) How do you remove duplicates values from an array?





2) Reverse the order of array elements




3) What's the result? <?php $arr = array(1,2,3,4,5); $arr = array_slice($arr, 2);






array_change_key_case() - Change keys to lower / upper <?php array_change_key_case($arr); // default CASE_LOWER array_change_key_case($arr, CASE_UPPER); 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_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 ) 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 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 )


References