PHP Array Functions

PHP provides a wide range of strong functions for effective array handling. Arrays are fundamental data structures in PHP, Which is used to store, organize, and manipulate data seamlessly. In this article, we’ll learn about PHP array functions & their use with the help of practical examples.

count()

It is use for count number of values in an array variable.

Syntax

count(array_variable, mode)

Here mode is optional parameter. Two modes are here: 0 & 1. By default, 0 works

  • 0 means, it will count only array value but will not count multidimensional values.
  • 1 means, it will count all values with multidimensional
$studentData = array('Suresh Kumar'=>array(22, 550), 'Sarita Khanna'=>array(24, 650), 'Mohan Sinha'=>array(20, 570), 'Kamlesh'=>array(26, 620));

echo "Without mode: ".count($studentData).'';
echo "With mode: ".count($studentData, 1);

Answer will be:

Without mode: 4
With mode: 12

end()

It is use to display last element in an array.

Syntax

end(array_variable)

$student = array('Suresh Kumar', 'Sarita Khanna', 'Mohan Sinha', 'Kamlesh');
echo end($student);

Answer will be:

Kamlesh

current()

It is use to display first element in an array.

Syntax

current(array_variable)

$student = array('Suresh Kumar', 'Sarita Khanna', 'Mohan Sinha', 'Kamlesh');
echo current($student);

Answer will be:

Suresh Kumar

array_sum()

It will sum all the values in an array.

Syntax

array_sum(array_variable)

$makrs = array(50, 40, 60, 70, 90);
echo array_sum($makrs);

Answer will be: 310

array_combine()

It combines two array and make new array as pair of key and value. The first array works as key and second array works as value.

Syntax

array_combine(array_variable1, array_variable2)

$studentName = array('Amit', 'Mahesh', 'Sahil', 'Rohit', 'Komal');
$marks = array(50, 40, 60, 70, 90);
echo "<pre>";
print_r(array_combine($studentName, $marks));
echo "</pre>";

Answer will be:

Array
(
[Amit] => 50
[Mahesh] => 40
[Sahil] => 60
[Rohit] => 70
[Komal] => 90
)

array_merge()

It will merge two or more array variables in one array variable. Here value of the second array variable always append at end of first array variable.

Syntax

array_merge(array_variable1, array_variable2)

$studentName = array('Amit', 'Mahesh', 'Sahil', 'Rohit', 'Komal');
$marks = array(50, 40, 60, 70, 90);

echo "<pre>";
print_r(array_merge($studentName, $marks));

Answer will be:

Array
(
[0] => Amit
[1] => Mahesh
[2] => Sahil
[3] => Rohit
[4] => Komal
[5] => 50
[6] => 40
[7] => 60
[8] => 70
[9] => 90
)

array_diff()

It compares two or more array variable and always returns the value from the first array variable, which is not present in any of the other array variable.

Syntax

array_diff(array_variable1, array_variable2)

$studentName1 = array('Amit', 'Mahesh', 'Sahil', 'Rohit', 'Komal');
$studentName2 = array('Amit', 'Mahesh', 'Mohit', 'Rohit', 'Komal');

echo "<pre>";
print_r(array_diff($studentName1, $studentName2));
echo "</pre>";

Answer will be:

Array
(
[2] => Sahil
)

array_flip()

It use to interchange the key and values of an array variable. Here key will make value and value will make key.

Syntax

array_flip(array_variable)

$array_data = array('Sunil'=>25, 'Mukesh'=>24, 'Dinesh'=>34, 'Kamlesh'=>15);
$new_array_data = array_flip($array_data);

echo "<pre>";
print_r($new_array_data);
echo "</pre>";

Answer will be:

Array
(
[25] => Sunil
[24] => Mukesh
[34] => Dinesh
[15] => Kamlesh
)

array_pop()

It removes last element of an array. If array variable will be empty, then it will return NULL.

Syntax

array_pop(array_variable)

$array_data = array('Cat', 'Dog', 'Lion', 'Bird');
array_pop($array_data);

echo "<pre>";
print_r($array_data);
echo "</pre>";

Answer will be:

Array
(
[0] => Cat
[1] => Dog
[2] => Lion
)

array_push()

It will appends given values at end of an array. We can add more than one values at a time.

Syntax

array_push(array_variable, value1, value2,…)

$array_data = array('Cat', 'Dog');
array_push($array_data , 'Lion', 'Fox', 'Goat');

echo "<pre>";
print_r($array_data);
echo "</pre>";

Answer will be:

Array
(
[0] => Cat
[1] => Dog
[2] => Lion
[3] => Fox
[4] => Goat
)

array_slice()

It will gave you slice of an array from where you want. Here we can give start point(from where slice will start) and length(how much will slice). If we will give only start then it will start slicing from given point to till end of the array.

Syntax

array_slice(array_variable, start, length)

$array_data = array('Cat', 'Dog', 'Lion', 'Fox', 'Goat');
$new_array_data = array_slice($array_data , 2, 2);

echo "<pre>";
print_r($new_array_data);
echo "</pre>";

Answer will be:

Array
(
[0] => Lion
[1] => Fox
)

Also read about PHP Array

array_unique()

It removes duplicate values from an array variable.

Syntax

array_unique(array_variable)

$array_data = array('Cat', 'Dog', 'Cat', 'Fox', 'Dog');
$new_array_data = array_unique($array_data);

echo "<pre>";
print_r($new_array_data);
echo "</pre>";

Answer will be:

Array
(
[0] => Cat
[1] => Dog
[3] => Fox
)

sort()

It will sort array values in ascending order.

Syntax

sort($array_variable)

$array_data = array('Dog', 'Cat', 'Fox', 'Lion');
sort($array_data);

echo "<pre>";
print_r($array_data);
echo "</pre>";

Answer will be:

Array
(
[0] => Cat
[1] => Dog
[2] => Fox
[3] => Lion
)

in_array()

It will search the given string is available in array or not. It is case-sensitive.

Syntax

in_array(search_string, array_variabale)

$array_data = array('Dog', 'Cat', 'Fox', 'Lion');
if(in_array('Dog', $array_data))
{
     echo "Found";
}
else
{
     echo "Not Found";
}

Answer will be: Found

A key component in making array manipulation chores in web development simpler is the use of PHP array functions. These routines offer effective solutions to basic array operations, whether you’re working on dynamic web applications, processing form data, or connecting with databases. Through the utilization of PHP array methods, developers may simplify data handling, improve code efficiency, and create scalable, reliable systems with ease.

Categorized in: