Useful Array functions in PHP

There are around 80 different functions related to arrays. The complete list can be found at http://php.net/manual/en/book.array.php.

We are going to run into  PHP array functions with amazing regularity during the course of your web design and web development. It makes sense, because arrays are one of the most useful data types we can use.

Some of the important ones are as follows:

  • array_key_exists — Checks if the given key or index exists in the array
  • array_keys — Return all the keys or a subset of the keys of an array
  • array_merge — Merge one or more arrays
  • array_multisort — Sort multiple or multi-dimensional arrays
  • array_pad — Pad array to the specified length with a value
  • array_pop — Pop the element off the end of array
  • array_product — Calculate the product of values in an array
  • array_push — Push one or more elements onto the end of array
  • array_rand — Pick one or more random keys out of an array
  • array_reduce — Iteratively reduce the array to a single value using a callback function
  • array_replace_recursive — Replaces elements from passed arrays into the first array recursively
  • array_replace — Replaces elements from passed arrays into the first array
  • array_reverse — Return an array with elements in reverse order
  • array_search — Searches the array for a given value and returns the first corresponding key if successful
  • arsort — Sort an array in reverse order and maintain index association
  • asort — Sort an array and maintain index association
  • compact — Create array containing variables and their values
  • count — Count all elements in an array, or something in an object
  • current — Return the current element in an array
  • each — Return the current key and value pair from an array and advance the array cursor
  • end — Set the internal pointer of an array to its last element
  • extract — Import variables into the current symbol table from an array
  • in_array — Checks if a value exists in an array
  • key_exists — Alias of array_key_exists
  • key — Fetch a key from an array
  • krsort — Sort an array by key in reverse order
  • ksort — Sort an array by key
  • list — Assign variables as if they were an array
  • natsort — Sort an array using a “natural order” algorithm
  • next — Advance the internal pointer of an array
  • pos — Alias of current
  • prev — Rewind the internal array pointer
  • range — Create an array containing a range of elements
  • reset — Set the internal pointer of an array to its first element
  • rsort — Sort an array in reverse order
  • shuffle — Shuffle an array
  • sizeof — Alias of count
  • sort — Sort an array
  • uasort — Sort an array with a user-defined comparison function and maintain index association
  • uksort — Sort an array by keys using a user-defined comparison function
  • usort — Sort an array by values using a user-defined comparison function

We can get a list of the keys of the array with array_keys, and a list of its values with array_values:

<?php
$properties = [
'firstname' => 'Tom',
'surname' => 'Riddle',
'house' => 'Slytherin'
];
$keys = array_keys($properties);
var_dump($keys);
$values = array_values($properties);
var_dump($values);
We can get the number of elements in an array with the count function:
<?php
$names = ['Harry', 'Ron', 'Hermione'];
$size = count($names);
var_dump($size); // 3

And we can merge two or more arrays into one with array_merge:

<?php
$good = ['Harry', 'Ron', 'Hermione'];
$bad = ['Dudley', 'Vernon', 'Petunia'];
$all = array_merge($good, $bad);
var_dump($all);
The last example will print the following array:
array(6) {
[0]=>
string(5) "Harry"
[1]=>
string(3) "Ron"
[2]=>
string(8) "Hermione"
[3]=>
string(6) "Dudley"
[4]=>
 string(6) "Vernon"
[5]=>
string(7) "Petunia"
}

As we can see, the keys of the second array are now different, as originally, both the arrays had the same numeric keys, and an array cannot have two values for the same key.

Leave a Comment

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