Most Useful Array functions in PHP

Updated on     Kisan Patel

In this tutorial, we have hand-picked useful array functions that I think will be most useful for you in PHP.

array_push function

array_push is the quickest way to add one or more items to the end of the array:

$friends = array("Kisan", "Ravi");
array_push($friends, "Devang");
var_dump($friends);

php-array_push-function

array_pop function

array_pop is the opposite of array_push: it pulls the last item off of the array and returns it.

$friends = array("Kisan", "Ravi");
array_pop($friends); // Delete Ravi from array
var_dump($friends);

php-array_pop-function

array_unshift function

array_unshift function adds an item (or several) to the beginning of the array.

$friends = array("Kisan", "Ravi");
array_unshift($friends, "Devang", "Ujas");
var_dump($friends);

php-array_unshift-function

array_shift function

array_shift pulls the first item off the front of the array and returns it.

$friends = array("Kisan", "Ravi");
var_dump(array_shift($friends));

php-array_shift-function

count function

count function simply takes an array as a parameter and returns the total number of items there are in the array.

$friends = array("Kisan", "Ravi", "Devang", "Ujas", "Jay");
echo 'Number of friends: ' .count($friends);

php-count-function


PHP

Leave a Reply