Searching...
Friday 5 July 2013

Adding and Removing Array Elements


PHP provides a number of functions for both growing and shrinking an array (what is an array?). Some of these functions are provided as a convenience to programmers who wish to mimic various queue implementations (FIFO, LIFO, etc.), as reflected by their names (push, pop, shift, and unshift). In this article we’ll be covering these topics and many more with examples.
Note:- A traditional queue is a data structure in which the elements are removed in the same order in which they were entered, known as first-in-first-out or FIFO. In contrast, a stack is a data structure in which the elements are removed in the order opposite to that in which they were entered, known as last-in-first-out or LIFO.

Adding a Value to the Front of an Array

The array_unshift() function adds elements to the front of the array. Returns the new number of elements in the array.  All preexisting numerical keys are modified to reflect their new position in the array, but associative keys aren’t affected. Its prototype follows:

int array_unshift(array array, mixed variable [, mixed variable...])

The following example adds two states to the front of the $states array:

$states = array("Ohio", "New York");
array_unshift($states, "California", "Texas");
// $states = array("California", "Texas", "Ohio", "New York");

Adding a Value to the End of an Array

The array_push() function adds a value to the end of an array, returning the total count of elements in the array after the new value has been added. You can push multiple variables onto the array simultaneously by passing these variables into the function as input parameters. Its prototype follows:

int array_push(array array, mixed variable [, mixed variable...])

Note:- If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.
Note:- array_push() will raise a warning if the first argument is not an array. This differs from the $var[] behaviour where a new array is created.

If you're going to use array_push() to insert a "$key" => "$value" pair into an array, it can be done using the following:

    $data[$key] = $value;

It is not necessary to use array_push.

The following example adds two more states onto the $states array:

$states = array("Ohio", "New York");
array_push($states, "California", "Texas");
// $states = array("Ohio", "New York", "California", "Texas");

Removing a Value from the Front of an Array

The array_shift() function removes and returns the first item found in an array, or NULL if array is empty or is not an array.  If numerical keys are used, all corresponding values will be shifted down, whereas arrays using associative keys will not be affected. Its prototype follows:

mixed array_shift(array array)

The following example removes the first state from the $states array:
$states = array("Ohio", "New York", "California", "Texas");
$state = array_shift($states);
// $states = array("New York", "California", "Texas")
// $state = "Ohio"

Note:- If you want a version of array_shift() that works non-destructively (i.e., an easy function to grab the first element of the array without modifying the array), try reset(), which returns the value of first element or False if array is empty.

Removing a Value from the End of an Array

The array_pop() function removes and returns the last element from an array. If array is empty (or is not an array), NULL will be returned.  Its prototype follows:

mixed array_pop(array array)

The following example removes the last state from the $states array:

$states = array("Ohio", "New York", "California", "Texas");
$state = array_pop($states);
// $states = array("Ohio", "New York", "California"
// $state = "Texas"

Note:- If you want a version of array_pop() that works non-destructively (i.e., an easy function to grab the last element of the array without modifying the array), try end(), which returns the value of last element or False if array is empty.

You can see a single task can be accomplished by a number of ways. It depends on your comfort and demand of the situation- which method to choose.


0 comments:

Post a Comment

 
Back to top!