Expert Refresh

Operations

1) You are checking to see if an array key exists. Which is correct?




2) Which array function do you use for the required result? <?php $a = array('a', 'b'); $b = ... print_r($b); // Array ( [0] => b [1] => a )





3) To advance the pointer of an array, what do you use?





4) Is it bad to use foreach with variable reference? <?php foreach($arr as &$v): ?>







Is_array() is used to check if an array exists (don't use count()). <?php $a = array(1,2,3); $b = array(); $c = 2; echo count($a); // Output: 3 echo count($b); // Output: 0 echo count($c); // Output: 1 echo is_array($c); // False // --- Look Here --- // Array_key_exists() is used to check if a key exists (don't use not isset()). <?php $a = array ('a' => NULL, 'b' => 2); echo isset ($a['a']); // False // --- Look Here --- // echo array_key_exists('a', $a); // True Array_flip() inverts values with the keys. <?php $a = array('a', 'b'); $b = array_flip($a); print_r($b); // Array ( [a] => 0 [b] => 1 ) // --- Look Here --- // Array_reverse() inverse the order of the arrray's elements. <?php $a = array('a', 'b'); $b = array_reverse($a); print_r($b); // Array ( [0] => b [1] => a ) // --- Look Here --- // Reset(), Key(), Next(), Current() are used when working with internal array pointer. <?php $arr = array('a' => 1, 'b' => 2, 'c' => 3); reset($arr); while (key($arr) !== NULL) { echo key($arr) . current($arr) . " "; next($arr); } // Output: a1 b2 c3 // --- Look Here --- // End() - You could start your iteration from the last element. <?php $arr = array('a' => 1, 'b' => 2, 'c' => 3); end($arr); // --- Look Here --- // while (key($arr) !== NULL) { echo key($arr) . current($arr) . " "; prev($arr); } // Output: c3 b2 a1 With PHP 5 the value of an array can be change by reference. <?php $a = array (1, 2, 3); foreach ($a as $k => &$v) { // --- Look Here --- // $v *= 2; } var_dump ($a); // array(3) { [0]=> int(2) [1]=> int(4) [2]=> &int(6) } 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,4); function setDouble($value) { echo $value * 2; } array_walk($arr, 'setDouble'); // output 2468 // Common use is passing value by reference in function definition. $arr = array(1,2,3,4); function setDouble(&$value) { $value *= 2; } // --- Look Here --- // array_walk($arr, 'setDouble'); print_r($arr); // Array ( [0] => 2 [1] => 4 [2] => 6 [3] => 8 ) Sort(), Rsort() destroys all the keys and rearange elements. <?php $arr = array('a'=>'foo', 'b'=>'bar', 'c'=>'baz'); sort($arr); print_r($arr); // --- Look Here --- // // Array ( [0] => bar [1] => baz [2] => foo ) Natsort() maintans all the key-value associations. <?php // unlike sort(), maintain all the key-value associations in the array $arr = array('10t', '2t', '3t'); natsort($arr); print_r($arr); // Array ( [1] => 2t [2] => 3t [0] => 10t ) Asort(), Arsort() are used in case of associative array. <?php $arr = array('a'=>'foo', 'b'=>'bar', 'c'=>'baz'); asort($arr); print_r($arr); // --- Look Here --- // // Array ( [b] => bar [c] => baz [a] => foo ) Ksort(), Krsort() sorts by key. <?php // sort by key (rather than by value) $arr = array('a'=>'foo', 'c'=>'bar', 'b'=>'baz'); ksort($arr); print_r($arr); // --- Look Here --- // // Array ( [a] => foo [b] => baz [c] => bar ) Usort(), Uasort() sorts by custom rules. <?php $arr = array('three', '2two', 'one', 'two'); function myCmp($left, $right) { if ($diff = strlen($left) - strlen($right)) { return $diff; } return strcmp($left, $right); } usort($arr, 'myCmp'); // --- Look Here --- // print_r($arr); // Array ( [0] => one [1] => two [2] => 2two [3] => three ) Arrays are often used as stacks: LILO: Last In, Last Out FIFO: First In, First Out Array_push(), Array_pop() - add at end / extract last <?php // add two elements to array (array_push) $arr = array(); array_push($arr, 'bar', 'baz'); // --- Look Here --- // print_r($arr); // Array ( [0] => bar [1] => baz ) // extract the last element added to the array (array_pop) $lastIn = array_pop($arr); // --- Look Here --- // var_dump($lastIn, $arr); // string(3) "baz" array(1) { [0]=> string(3) "bar" } $arr[] = 'foo'; // when only one value if being pushed // faster than array_push (no function call) Array_shift(), Array_unshift() - extract first, add to begining <?php // push the first element out of the array (array_shift) $arr = array('bar', 'baz'); $firstElement = array_shift($arr); //print_r($arr); // Array ( [0] => baz ) // add a value to the beginning of the array (array_unshift) array_unshift($arr, 'foo'); print_r($arr); // Array ( [0] => foo [1] => baz ) Array_diff() - keeps only diff value <?php $a = array (1, 2, 3); $b = array (1, 3, 4); print_r(array_diff ($a, $b)); // Array ( [1] => 2 ) // --- Look Here --- // Array_intersect() - keeps only identical values <?php $a = array (1, 2, 3); $b = array (1, 3, 4); print_r(array_intersect ($a, $b)); // --- Look Here --- // // Array ( [0] => 1 [2] => 3 ) String to array <?php $str = "111'; $arr = str_split($str);


References