Tuesday 14 August 2018

PHP Array Push Pop Shift and Unshift

OVERVIEW

Many if not all PHP developers will come across arrays in their day to day programming. Arrays allow you to store ‘rows’ or ‘blocks’ of information. PHP comes with plenty functions that can be used to create, manage and simplify the use of arrays. I will focus on four functions that allow you to add and remove elements from an array. I will also show you how to access the first and last elements of an array using these functions.

PHP ARRAY FUNCTIONS

  • array_push()
  • array_pop()
  • array_shift()
  • array_unshift()

PHP ARRAY PUSH POP SHIFT AND UNSHIFT

For each of the four functions, I will give a short overview of what each one is used for as well as alternative functions that can be used to achieve the same result. Remember that each of the four functions will reset the internal pointer for the array when used causing your ‘foreach’ function to start at the beginning of the array.

PHP PUSH ARRAY – ARRAY_PUSH()

Add one or more elements onto the end of array. Returns the number of elements in the new array.

Standard method using array_push();

  1. // create our array with 1 element
  2. $arr = array("one");
  3. // $count will be 3 and $arr will now be array("one","two","three");
  4. $count = array_push($arr,"two","three");

Alternate method using $arr[]

tip! Using this method can be faster than using array_push since there is no overhead of calling an actual function.
  1. // create our array with 1 element
  2. $arr = array("one");
  3. // $arr will now be array("one","two");
  4. $arr[] = "two";
  5. // $arr will now be array("one","two","three");
  6. $arr[] = "three";
  7. // $count will be 3
  8. $count = count($arr);

Alternate method using array_merge();

  1. // create our array with 1 element
  2. $arr = array("one");
  3. // alternate method using array_merge()
  4. $arr = array_merge($arr,array("two","three")); // $arr will now be array("one","two","three");
  5. $count = count($arr); // $count will be 3

PHP POP ARRAY – ARRAY_POP()

Removes and returns the value of the last element of an array. Returns NULL if array is empty or not set.

Standard method using array_pop();

  1. // create our array with 3 elements
  2. $arr = array("one","two","three");
  3. // $value will be "three" and array's value will now be array("one","two");
  4. $value = array_pop($arr);

Alternate method using array_slice();

  1. // create our array with 3 elements
  2. $arr = array("one","two","three");
  3. // alternative method using array_slice()
  4. $value = $arr[count($arr)-1]; // $value will no be "three"
  5. $arr = array_slice($arr, 0, -1);   // $arr will now be array("one","two");

PHP SHIFT ARRAY – ARRAY_SHIFT()

Removes and returns the value of the first element of an array. Returns NULL if array is empty or not set.

Standard method using array_shift();

  1. // create our array with 3 elements
  2. $arr = array("one","two","three");
  3. // $value will be "one" and array's value will now be array("two","three");
  4. $value = array_shift($arr);

Alternate method using array_slice();

  1. // create our array with 3 elements
  2. $arr = array("one","two","three");
  3. $value = $arr[0]; // $value will now be "one"
  4. $arr = array_slice($arr, 1); // array's value will now be array("two","three");

PHP UNSHIFT ARRAY – ARRAY_UNSHIFT()

Add one or more elements to the beginning of an array. Returns the number of elements in the new array.

Standard method using array_unshift();

  1. // create our array with 3 elements
  2. $arr = array("three","four","five");
  3. // $count will now be 5 and array will hold one - five
  4. $count = array_unshift($arr,"one","two");

Alternate method using array_slice();

  1. // create our array with 3 elements
  2. $arr = array("three","four","five");
  3. // $count will now be 5 and array will hold one - five
  4. $arr = array_merge(array("one","two"),$arr);
  5. $count = count($arr);

0 comments:

Post a Comment