Useful PHP Functions To Work With Arrays

pradeep's Avatar author of Useful PHP Functions To Work With Arrays
This is an article on Useful PHP Functions To Work With Arrays in PHP.
The in-built array functions allow you to interact with and manipulate arrays in various ways. Arrays are essential for storing, managing, and operating on sets of variables.

Simple and multi-dimensional arrays are supported, and may be either user created or created by another function. There are specific database handling functions for populating arrays from database queries, and several functions return arrays.

You can read more about working with PHP arrays here.

In this article I'll demonstrate a few important and useful php functions, you can read and refer to the whole list of PHP functions on the official PHP website.

array_push()



Inserts one or more elements to the end of an array.

Code: PHP
$arr = array('Shabbir','Pradeep');
 
 //Add one more element
 array_push($arr,'Tanaz');
 
 print_r($arr);
 
 /*
 Output:
 Array
 (
     [0] => Shabbir
     [1] => Pradeep
     [2] => Tanaz
 )
 */

array_pop()



Deletes the last element of an array.

Code: PHP
$stack = array("orange", "banana", "apple", "raspberry");
 $fruit = array_pop($stack);
 print_r($stack);
 
 /*
 After this, $stack will have only 3 elements:
 Array
 (
     [0] => orange
     [1] => banana
     [2] => apple
 )
 
 and raspberry will be assigned to $fruit.
 */

array_shift()



Removes the first element from an array, and returns the value of the removed element.

Code: PHP
$stack = array("orange", "banana", "apple", "raspberry");
 $fruit = array_shift($stack);
 print_r($stack);
 
 /*
 This would result in $stack having 3 elements left:
 Array
 (
     [0] => banana
     [1] => apple
     [2] => raspberry
 )
 
 and orange will be assigned to $fruit.
 */

array_unshift()



Adds one or more elements to the beginning of an array.

Code: PHP
$queue = array("orange", "banana");
 array_unshift($queue, "apple", "raspberry");
 print_r($queue);
 
 
 /*
 Output
 Array
 (
     [0] => apple
     [1] => raspberry
     [2] => orange
     [3] => banana
 )
 */

array_unique()



Removes duplicate values from an array.array_unique() takes input array and returns a new array without duplicate values.

Code: PHP
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
 $result = array_unique($input);
 print_r($result);
 
 /*
 Output
 Array
 (
     [a] => green
     [0] => red
     [1] => blue
 )
 */

in_array()



Checks if a value exists in an array.

Code: PHP
$os = array("Mac", "NT", "Irix", "Linux");
 if (in_array("Irix", $os))
 {
     echo "Got Irix";
 }
 if (in_array("mac", $os))
 {
     echo "Got mac";
 }
 
 /*
 The second condition fails because in_array() is case-sensitive, so the program above will display:
 
 Got Irix
 */

array_walk()



Apply a user function to every member of an array.

Code: PHP
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
 
 function test_alter(&$item1, $key, $prefix)
 {
     $item1 = "$prefix: $item1";
 }
 
 function test_print($item2, $key)
 {
     echo "$key. $item2<br />\n";
 }
 
 echo "Before ...:\n";
 array_walk($fruits, 'test_print');
 
 array_walk($fruits, 'test_alter', 'fruit');
 echo "... and after:\n";
 
 array_walk($fruits, 'test_print');
 
 /*
 The above example will output:
 Before ...:
 d. lemon
 a. orange
 b. banana
 c. apple
 ... and after:
 d. fruit: lemon
 a. fruit: orange
 b. fruit: banana
 c. fruit: apple
 */

array_combine()



Creates an array by using one array for keys and another for its values.

Code: PHP
$a = array('green', 'red', 'yellow');
 $b = array('avocado', 'apple', 'banana');
 $c = array_combine($a, $b);
 
 print_r($c);
 
 /*
 The above example will output:
 
 Array
 (
     [green]  => avocado
     [red]    => apple
     [yellow] => banana
 )
 */

end()



Sets the internal pointer of an array to its last element.This function can be used to get the last element of an array.

Code: PHP
$fruits = array('apple', 'banana', 'cranberry');
 echo end($fruits); // cranberry
 
These functions make our work a bit more easier, I consider these are the most frequently used array functions, but this is not all that PHP offers, a lot more can be found at the PHP website.
trinitybrown's Avatar, Join Date: Oct 2008
Go4Expert Member
ALl scripts are very fine, do you have some idea about associative arrays and ksort() function as i tried to learn it but was unable to understand it , kindly help me on the topic
pradeep's Avatar, Join Date: Apr 2005
Team Leader
What exactly do you want to know?
happyz's Avatar, Join Date: Nov 2008
Go4Expert Member
Its a nice post abt PHP array functions
really like it
haileyjonathan's Avatar, Join Date: Mar 2009
Newbie Member
Hi Pradeep,
You wrote a wonderful scripts.It included PHP array function also. I appreciate to you..Thanks for your sharing also...
LenoxFinlay's Avatar
Banned
There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers, beginning at 0. Indexed arrays are used when you identify things by their position. Associative arrays have strings as keys and behave more like two-column tables. The first column is the key, which is used to access the value.PHP internally stores all arrays as associative arrays, so the only difference between associative and indexed arrays is what the keys happen to be. Some array features are provided mainly for use with indexed arrays, because they assume that you have or want keys that are consecutive integers beginning at 0. In both cases, the keys are unique--that is, you can't have two elements with the same key, regardless of whether the key is a string or an integer.PHP arrays have an internal order to their elements that is independent of the keys and values, and there are functions that you can use to traverse the arrays based on this internal order. The order is normally that in which values were inserted into the array, but the sorting functions described later let you change the order to one based on keys, values, or anything else you choose.
gkumar's Avatar
Banned
Like arrays in other languages, PHP arrays allow you to store multiple values in a single variable and operate on them as a set. PHP offers an extensive array manipulation toolkit—over 60 functions—that lets you process arrays in almost any way imaginable including reversing them, extracting subsets, comparing and sorting, recursively processing, and searching them for specific values.
his document outlines some of the more useful functions in the PHP array with explanations and usage examples:
Function:-
sizeof($arr)
array_values($arr)
array_keys($arr)
array_pop($arr)
array_push($arr, $val)
array_shift($arr)
array_unshift($arr, $val)
each($arr)
sort($arr)
array_flip($arr)
array_reverse($arr)
array_merge($arr)
array_rand($arr)
array_search($search, $arr)
array_slice($arr, $offset, $length)
array_unique($data)array_walk($arr, $func)
lrvv's Avatar, Join Date: Sep 2009
Newbie Member
ok,thx for your sharing
qforever's Avatar
Light Poster
Here is much more detailed description of PHP array functions: php.net/manual/en/book.array.php
qforever's Avatar
Light Poster
Just to kill this topic
loyo like this