Studyon Minte9.com
ZCE 5.3

Study

Operations



	// is_array() - to check array exists (not count())
	// array_key_exists() - to check keys (not isset())

	// array_reverse() - to inverts the order of elements
	// array_flip() - to invert values with keys

	// For iteration use key(), current(), next(), end(), prev()
	// Avoid using foreach and reference
	// array_walk() - Apply a user function to every member of an array

       // String to array - str_split($str)

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_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 )
  Array_map() - Returns a array containing elements of an array, after appling a callback function
<?php $arr = array(1,2,3); function setDouble($x) {return $x*2;} $arr2 = array_map("setDouble", $arr); var_dump($arr2); // Array (size=3) 0 => int 2, 1 => int 4, 2 => int 6
  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 )
  Shuffle() - randomizes the order of the elements.
<?php // Randomizes the order of the elements of the array $cards = array (1, 2, 3, 4); shuffle ($cards); // --- Look Here --- // var_dump ($cards); // random cards
  Array_rand() - extracts random element.
<?php // If you need to extract individual elements from the array at random $cards = array (1, 2, 3, 4); $keys = array_rand($cards, 2); // --- Look Here --- // print_r($keys); // Array ( [0] => 3 [1] => 1 ) randomly
  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);