Top 16 Most Useful PHP Array Functions

Top 16 Most Useful PHP Array Functions

Posted by

We are going to covered Top 16 Most Useful PHP Array Functions which frequently use in the development of PHP application or asked in the interview questions. These all are the in-built PHP functions.

  1. array_values()
  2. array_keys()
  3. array_key_exists()
  4. array_shift()
  5. array_unshift()
  6. array_push()
  7. array_pop()
  8. array_unique()
  9. array_slice()
  10. array_diff()
  11. array_search()
  12. array_reverse()
  13. array_flip()
  14. array_replace()
  15. is_array()
  16. in_array()

array_values() Function

We use array_valuees() function to get only values from the array. It does not return the key of the array.

Syntax:

Input: This function only takes one mandatory array parameter, from which values will be extracted.

Example:

Output: The function array_values() extract the values of an array.

array_keys() Function

We use the array_keys() function to extract the key of the array.

Syntax:

It takes three parameters and only the first parameter is required.

Input: It is an array, from which keys will be extracted.
Value: If you add value, then only the key of this value will be returned.
Strict: It is a boolean parameter and the default value is FALSE. If you set it TRUE then it works as a strict comparison operator(===).

Example:

Output: Here is showing the keys of the array.

Let’s take an example to passing value:

Output: It will return the keys of value 8000.

array_key_exists() Function

We use array_key_exists() function to check the specific key in an array. It returns TRUE if the key exists.

Syntax:

It takes two parameters:

Key: The key, you have to check.
Array: The key will be searched in this array.

Example:

array_shift() Function

It is used to remove the first element from an array and returns the removed element ‘s value.

Syntax:

It takes an array as a parameter.

Example:

Output: In this example, the array_shift() function returns the value of the first element and also removed the first element of the “$name” array.

array_unshift() Function

We use the array_shift() function to add one or more elements to the array. The new elements will be added at the beginning of the array.

Syntax:

Example: Here is taken an array of three elements and adding two new elements using the array_unshift() function.

Output: The new two elements are added at the beginning of the array.

array_push() Function

We use array_push() function to add one or more elements at the end of the array.

Syntax:

Array: This will be appended.
Values: The values will add to the array.

Example: Here is taken an example of an array that has three elements and adding two new elements using the array_push() function.

Output: Now two more elements have been pushed to the array at the end of the array.

array_pop() Function

We use array_pop() function to remove the last element of the array.

Syntax:

Example: In the below example, take an array with five elements.

Output: This function removed the last element from the array.

array_unique() function

We use the array_unique() function to remove duplicate value from an array.

Example: In this example, take 5 elements of the array and the value of the two-element is the same.

Output: The duplicate value has been removed.

array_slice() Function

We use the array_slice() function for slicing the array.

Syntax:

It accepts 4 arguments:

Array: This is the array for slicing.
Offset: The array start where the slicing is to take place. It displays location in the array. If Offset is negative (-1), then the slicing will start from the end of the array. The value -3 means that start at the array ‘s third last element.
Length: This is a slice length.
Preserve: It is a boolean parameter and the default value is FALSE. If you set it TRUE then it preserves the key values.

Example:

Output: This function slice the first two elements of the array and reset the key.

Let’s take another example:

Output: In this example, the function return values from 200, 50, and 300. The key has not reset because the argument of Preserve is TRUE.

array_diff() Function

We use the array_diff() function to compare the first array in parameters with the rest of the arrays and returns an array that contains all the first array entries that are not present in any of the other arrays.

Syntax:

Example:

Output: It has returned 22 because it is not present in $array2.

array_search() Function

We use the array_search() function to search a value in an array and it returns the key.

Syntax:

Value: This value to be search
Array: The value will be searched in this array.
Strict: It is a boolean parameter and the default value is FALSE. If you set it TRUE then it works as a strict comparison operator(===).

Example:

array_reverse() Function

We use the array_reverse() function to reverse the array.

Syntax:

It takes two parameters and the first is required.

Array: This array will be reversed.
Preserve: It is a boolean parameter and the default value is FALSE. If you set it TRUE then it preserves the key values.

Example: In this example, take four elements of the array.

Output: The elements of the array have been reversed.

Example:

Output: The value of preserve argument is TRUE so the key has not reset.

array_flip() Function

We use the array_flip() function to exchange the keys in an array with their corresponding values.

Syntax:

Example:

Output: It has exchange the key and value.

array_replace() Function:

We use the array_replace function to replace the first array value from the next array values if the key is matched. If the key does not match then it adds the new elements.

Syntax:

It accepts a minimum of two parameters:

Array: The values of this array will be replaced.
Array1: If the key will match from the first array then the function will replace the value otherwise the function will add those elements to the first array.

Let’s take an example:

Output: In the above example, the key A and B are matching in both array so the value of the first array has been replaced by the value of the second array and it has returned a new array after adding new elements C and D.

is_array() Function

We use the is_array() function to check whether a variable is an array or not. It returns TRUE or FALSE. You should use there where you do not know what kind of value will be set in this variable.

Syntax:

It accepts a variable for checking.

Example:

Output: The variable $var is an array so it is TRUE and output will be showing below.

Example:

Output: The variable $var is not an array so it will go in FALSE condition.

in_array() Function

We use the in_array() function to check the given value exists or not in an array. If the value exists then it will return TRUE otherwise it will return FALSE.

Syntax:

Value: This value to search in the Array
Array: In this array, the value will be search
Strict: It is a boolean parameter and the default value is FALSE. If you set it TRUE then it works as a strict comparison operator(===).

Example:

Output:

 

We have covered the Top 16 Most Useful PHP Array Functions in this article. If you wish to share more information or have a query about the topic Top 16 Most Useful PHP Array Functions, please write in the comments box.

Leave a Reply

Your email address will not be published. Required fields are marked *