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.
Inserts one or more elements to the end of an array.
Deletes the last element of an array.
Removes the first element from an array, and returns the value of the removed element.
Adds one or more elements to the beginning of an array.
Removes duplicate values from an array.array_unique() takes input array and returns a new array without duplicate values.
Checks if a value exists in an array.
Apply a user function to every member of an array.
Creates an array by using one array for keys and another for its values.
Sets the internal pointer of an array to its last element.This function can be used to get the last element of an array.
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.
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

