Top 10 important php array functions with examples and syntax

PHP is a versatile programming language widely used for developing web applications. One of its key features is its support for arrays, which makes it easy to store and manipulate data. In this article, we will discuss the top 10 important PHP array functions that are frequently used in PHP programming. We will explain each function’s syntax, parameters, return type, and provide examples, including optional parameters.

1. array_push()

The array_push() function is used to add one or more elements to the end of an array. The syntax for the function is as follows:

array_push($array, $value1, $value2, ...);

The $array parameter is the array to which we want to add elements. The $value1, $value2, etc. parameters are the values we want to add to the array. The return type is an integer, which represents the number of elements in the new array.

Example:

$fruits = array("apple", "banana");array_push($fruits, "orange", "grape");print_r($fruits);

Output:

Array ( [0] => apple [1] => banana [2] => orange [3] => grape )

2. array_pop()

The array_pop() function is used to remove the last element from an array. The syntax for the function is as follows:

array_pop($array); 

The $array parameter is the array from which we want to remove the last element. The return type is the value of the removed element.

Example:

$fruits = array("apple", "banana", "orange");$last_fruit = array_pop($fruits);print_r($fruits);echo $last_fruit;

Output:

Array ( [0] => apple [1] => banana )orange

Read also : Top 10 most used php string functions with examples and syntax

3. array_shift()

The array_shift() function is used to remove the first element from an array. The syntax for the function is as follows:

array_shift($array);

The $array parameter is the array from which we want to remove the first element. The return type is the value of the removed element.

Example:

$fruits = array("apple", "banana", "orange");$first_fruit = array_shift($fruits);print_r($fruits);echo $first_fruit;

Output:

Array ( [0] => banana [1] => orange )apple

4. array_unshift()

The array_unshift() function is used to add one or more elements to the beginning of an array. The syntax for the function is as follows:

array_unshift($array, $value1, $value2, ...);

The $array parameter is the array to which we want to add elements. The $value1, $value2, etc. parameters are the values we want to add to the beginning of the array. The return type is an integer, which represents the number of elements in the new array.

Example:

$fruits = array("apple", "banana");array_unshift($fruits, "orange", "grape");print_r($fruits);

Output:

Array ( [0] => orange [1] => grape [2] => apple [3] => banana )

Read Also : 10 important php functions with examples and explanation

5. array_slice()

The array_slice() function is used to extract a portion of an array. The syntax for the array_slice() function is as follows:

array_slice($array, $offset, $length, $preserve_keys)

Example:

$fruits = array("apple", "banana", "orange", "mango", "grape"); $sliced_fruits = array_slice($fruits, 1, 3); print_r($sliced_fruits);

Output:

Array ( [0] => banana [1] => orange [2] => mango )

6. array_splice()

The array_splice() function is used to remove and replace elements from an array. The syntax for the array_splice() function is as follows:

array_splice($array, $offset, $length, $replacement)

Example:

$fruits = array("apple", "banana", "orange", "mango", "grape"); array_splice($fruits, 2, 2, array("cherry", "kiwi")); print_r($fruits);

Output:

Array ( [0] => apple [1] => banana [2] => cherry [3] => kiwi )

7. array_merge()

The array_merge() function is used to merge two or more arrays into a single array. The syntax for the array_merge() function is as follows:

array_merge($array1, $array2, ...)

Example:

$fruits1 = array("apple", "banana", "orange"); $fruits2 = array("mango", "grape", "cherry"); $all_fruits = array_merge($fruits1, $fruits2); print_r($all_fruits);

Output:

Array ( [0] => apple [1] => banana [2] => orange [3] => mango [4] => grape [5] => cherry )

8. array_key_exists()

The array_key_exists() function is used to check if a given key exists in an array. The syntax for the array_key_exists() function is as follows:

array_key_exists($key, $array)

Example:

$fruits = array("apple" => 2, "banana" => 3, "orange" => 4); if (array_key_exists("banana", $fruits)) {     echo "Bananas exist in the array"; } else {    echo "Bananas do not exist in the array"; }

Output:

Bananas exist in the array

9. array_search()

The array_search() function is used to search for a value in an array and return its key if found. The syntax for the array_search() function is as follows:

array_search($value, $array, $strict)

Example:

$fruits = array("apple", "banana", "orange"); $position = array_search("banana", $fruits); echo $position;

Output:

1

10. array_reverse()

The array_reverse() function is used to reverse the order of elements in an array. The syntax for the array_reverse() function is as follows:

array_reverse($array, $preserve_keys)

Example:

$fruits = array("apple", "banana", "orange"); $reversed_fruits = array_reverse($fruits); print_r($reversed_fruits);

Output:

Array ( [0] => orange [1] => banana [2] => apple )

Conclusion

Arrays are an essential part of PHP development, and developers need to understand how to manipulate them effectively. In this article, we have discussed the top 10 most important PHP array functions with detailed explanations, syntax, and examples. By utilizing these functions, developers can easily perform complex operations with arrays and optimize their code for better performance.

Comments