Studyon Minte9.com
ZCE 5.3

Study

Search



in_array() - Check if a value exists in an array

<?php $array = array('a'=>1, 'b'=>2, 'c'=>3); echo in_array(1, $array); // Output: 1
  array_key_exists() - Checks if the given key exists in the array
<?php $arr = array ('a' => NULL, 'b' => 2); echo isset ($arr['a']); // False echo array_key_exists('a', $arr); // True
  array_sum() - sum of values in an array
<?php $arr = array(1,2,3); echo array_sum($arr); // 6
  array_product() - factorial
<?php $n = 3; echo array_product(range(1, $n)); // Ouput: 6 = 3! = 1*2*3
  array_search() - searches value, returns key
<?php // May return boolean false (always use indentical operator) $array = array(1,2,3,4); echo array_search(4, $array); // Output 3 // Wrong if (array_search(5, $array) == 0) { // !! true echo 'Found'; // Output: Found! } // Corect if (array_search(5, $array) === 0) { // false echo 'Found'; // Output nothing }
  array_keys() - Return all the keys
<?php // If the optional search_value is specified, only the keys for that value are returned $arr = array('a', 'b', 'a', 'c', 'a', '0', false); print_r(array_keys($arr)); // [ 0, 1, 2, 3, 4, 5, 6 ] // search_value provided print_r(array_keys($arr, 'a')); // [ 0, 2, 4 ] // strict search (identical comparation) print_r(array_keys($arr, '0')); // [ 5, 6 ] !! not true print_r(array_keys($arr, '0', true)); // [ 5 ] corect
  count() - Count all elements in an array
<?php echo count(array(1,2,3)); // Output: 3 // For a variable that isn't set echo count($arr); // Output: 0 // Caution for false echo count(false); // Output: 1
  key, current, next, reset
<?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
  end, prev
<?php // You could start your iteration from the last element $arr = array('a' => 1, 'b' => 2, 'c' => 3); end($arr); while (key($arr) !== NULL) { echo key($arr) . current($arr) . " "; prev($arr); } // Output: c3 b2 a1