Expert Refresh

Stacks

1) Remove the first element of an array





2) Add element to the begining of an array





3) Add element at the end of an array





4) Extract the last element of an array






array_push() - add at end <?php // Example 1 $arr = array(); array_push($arr, 'bar', 'baz'); print_r($arr); // Array ( [0] => bar [1] => baz ) // Example 2 $arr[] = 'foo'; // when only one value if being pushed // faster than array_push (no function call) array_pop() - extract last element <?php $arr = array('bar', 'baz'); $lastIn = array_pop($arr); var_dump($lastIn, $arr); // string(3) "baz" array(1) { [0]=> string(3) "bar" } array_shift() - remove first <?php $arr = array('bar', 'baz'); $firstElement = array_shift($arr); //print_r($arr); // Array ( [0] => baz ) array_unshift() - add to begining <?php $arr = array('baz'); array_unshift($arr, 'foo'); print_r($arr); // Array ( [0] => foo [1] => baz )


References