• SECTION
  • Please insert widget into sidebar Customize Righ Home 1 in Appearance > Widgets

PHP arrays are grouping of multiple variables under one name. Or to put it a different way, arrays store multiple data inputs under one variable. For example:

$arrayVar=array(0 => 'input1', 1 => 'input2');
 
// echos input1
echo $arrayVar[0];
 
// echos input2
echo $arrayVar[1];

In the above example, $arrayVar is a single variable, yet it is holding two inputs. However, because an array in PHP can have strings as both inputs and keys, as well numerical values for each, there exists functions designed specifically for operating on the key/index of an array. Likewise, there exists a multitude of functions for the values of an array as well as the ordering of the array itself. Below is a list of the most important functions for arrays, along with usage examples. Some of these examples make use of print_r(), which is a nice and easy way to output the structure and content of an array. Also note that underneath each function/operation heading the PHP version required for the function to exist is noted. All inputs to these functions must be an array, as these are all operations on arrays.

array
PHP version 4 and up

To start off, lets start with a basic function that actually creates an array. You feed this function a list separated by commas to create an array with values composed of that list. The keys for this list will be numeric, starting from 0 and incrementing each time a new element is added. However, if you want you can also specify the keys of the array as well by first stating the key value then using => after to specify the value.
usage:

$lastNames=array('Smith','Johnson','Neal');
 
print_r($lastNames);
 
/*
Will output
array
(
[0] => Smith
[1] => Johnson
[2] => Neal
)
*/
$fullNames=array('John' => 'Smith','Tim' => 'Johnson','Ken' => 'Neal');
 
print_r($fullNames);
 
/*
Will output
array
(
[John] => Smith
[Tim] => Johnson
[Ken] => Neal
)
*/

array_change_key_case
PHP version 4.2.0 and up

If you have an array where there are strings used for the keys, you can use this function to ensure that they are all upper or lower case, whichever your application needs. In general, you should make sure to not use mixed case keys. However, if you are working with a third party script or some mangled data this may occur.
usage:

$arrayVar=array('MiXeDCAseKey' => 3);
// After this, the key will be 'mixedcasekey'
$arrayVar=array_change_key_case($arrayVar, CASE_LOWER);
 
// After this, the key will be 'MIXEDCASEKEY'
$arrayVar=array_change_key_case($arrayVar, CASE_UPPER);

array_combine
PHP version 5.0 and up

If you have two arrays that are synced, you can combine them into one array, where one acts as the key and the other acts as a value. For example, you have two sets of data, one contains last names, another contains the first names, and they’re both ordered the same (person A’s first name in the first name array aligns with their last name in the last name array).
usage:

$lastNames=array('Smith','Johnson','Neal');
$firstNames=array('John','Tim','Ken');
 
$firstLastNames=array_combine($firstNames,$lastNames)
 
foreach($firstLastNames as $key => $value)
{
echo $key.' '.$value;
}
 
/* above code outputs
John Smith
Tim Johnson
Ken Neal
*/

array_count_values
PHP version 4 and up

Imagine you have a text file, you want to find the number of instances for each word. How would you go about this? Well, one easy method is to take the text, split it into an array via explode(), and then pass it through array_count_values. The function array_count_values looks at how many times a value in the input array occurs, then outputs an array with the input values as the key, and the number of times the input value occurred as the value.
usage:

$arrayVar=array('test', 'input', 'value', 'value', 'test');
$arrayCount=array_count_values($arrayVar);
print_r($arrayCount);
 
/* above code outputs
array 
(
[test] => 2 
[input] => 1 
[value] => 2 
)
*/

array_diff
PHP version 4.0.1 and up

If you have two arrays and you want to see what to see differences you can use array_diff. For example, if you have an array, and you run some operations on it that will delete a few entries and save the result as a new array, and you want to see which entries have been affected you can do just that.
usage:

$arrayVar1=array('test', 'input', 'value', 'value', 'test');
$arrayVar2=array('test', 'value', 'test');
$arrayDiff=array_diff($arrayVar);
print_r($arrayDiff);
 
/* above code outputs
array 
(
[1] => 'input' 
)
*/

Since the entry, ‘input’, was deleted in the second array, the array_diff returned ‘input’ as it exists in the original, but not the altered array.

array_filter
PHP version 4.0.6 and up

Imagine that you have an array generated by a third party source, perhaps this source includes a mixture of strings and integers in it’s values, but you want you only use the values that are integers as it will interfere with your code otherwise. How would you do this? Well, one option is array_filter, which will loop through an array and only return the values that meet the criteria you set in a callback function.
usage:

function intCheck($var)
{
return is_int($var);
}
 
$arrayVar=array('test', 1, 'value', '12', 5);
$arrayFilter=array_filter($arrayVar, "intCheck");
 
print_r($arrayFilter);
 
/* above code outputs
array 
(
[1] => 1 
[4] => 5
)
*/

array_key_exists
PHP version 4.0.7 and up

If you have an array that you’re adding values to, using a unique key that is derived from a procedural source, how would you go about making sure you don’t overwrite the value of that key with a duplicate? Well, an easy way is to test if the key already exists. This is where array_key_exists comes in handy.
usage:

$arrayVar=array('john' => 35, 'tom' => 22, 'ken' => 73);
 
// Code below echos 'true'
if(array_key_exists($arrayVar,'ken'))
{
echo 'true';
}
else
{
echo 'false';
}

Here the first argument is your array to test, and the second argument is the value to search for. Keep in mind that you can also feed another array as a second argument, which allows you to place all of your key search variations (if you have multiple) into a single variable.

array_keys
PHP version 4 and up

If you find yourself wanting a list of all the keys that an array contains, you can use this array_keys. The function takes an array and returns an array that lists the keys of the input array.
usage:

$arrayVar=array('john' => 35, 'tom' => 22, 'ken' => 73);
print_r(array_keys($arrayVar));
 
/*
Above code outputs
array
(
[0] => 'john'
[1] => 'tom'
[2] => 'ken'
)
*/

array_values
PHP version 4 and up

Just as array_keys will output an array of all keys in an array, array_values will output all values in an array as an array. This can be useful if you want to loop through an array using it’s numerical key/index value instead of a string key value.
usage:

$arrayVar=array('john' => 35, 'tom' => 22, 'ken' => 73);
print_r(array_values($arrayVar));
 
/*
Above code outputs
array
(
[0] => '35'
[1] => '22'
[2] => '73'
)
*/

array_pop
PHP version 4 and up

Say for example you have some code that generates an array, but the code always adds an extra entry. You could alter the logic of your code, but say you don’t want to, or cannot for some reason. Well, in that case you can use array_pop. Popping refers to a stack operation, most often remembered from the days of assembly programming, an array is thought of as a stack of values, and popping an element means to remove it from the stack. Thus, you can use array_pop to ‘pop’ the last element of an array out of the array, essentially removing it.
usage:

$arrayVar=array('john', 'tom', 'ken');
print_r(array_pop($arrayVar));
 
/*
Above code outputs
array
(
[0] => 'john'
[1] => 'tom'
)
*/

array_push
PHP version 4 and up

Just as array_pop, pops out an element from an array, array_push, pushes an element onto the array. The first argument is the original array you want to push to, and the rest are the values to push.
usage:

$arrayVar=array('john', 'tom', 'ken');
print_r(array_push($arrayVar, 'tim'));
 
/*
Above code outputs
array
(
[0] => 'john'
[1] => 'tom'
[2] => 'ken'
[3] => 'tim'
)
*/

array_product
PHP version 5.1.0 and up

Sometimes it may be convenient to throw all of the values you want to multiply a product from into an array. Say for example you’re looping through a bunch of values and you wish to store those values in an array and do various calculations with them later. If one of those calculations is a product result, then you can use array_product.
usage:

$arrayVar=array(1, 28, 73);
echo array_product($arrayVar);
 
/*
Above code outputs 2044, as 1*28*73=2044
*/

array_sum
PHP version 4.0.4 and up

Just as you have array_product to calculate the product of the elements of an array, you also have array_sum to add all of the elements of an array.
usage:

$arrayVar=array(1, 28, 73, 17, 54);
// will return 173
echo array_sum($arrayVar);

array_rand
PHP version 4 and up

Imagine you have a directory on your server that holds images that are to be “featured” on your home page. Say you want to randomly select one to show each time a page is loaded, how would you go about doing that? One option would be to look into the directory, grab a list of file names and add them to an array, then randomly pick from the array. Randomly picking from an array can be accomplished via array_rand. This function will randomly select an element or elements from an array. If you pick one value, a single value is returned, if you pick more than one the result returned is an array with the results.
usage:

$arrayVar=array(1, 28, 73, 17, 54);
// will return a single element, like 17 for example
echo array_rand($arrayVar,1);
 
print_r(array_rand($arrayVar,3));
/*
Above code will return an array of 3 elements, 
one such output could be:
 
array
(
[0] => 28
[1] => 1
[2] => 17
)
*/

array_reverse
PHP version 4 and up

Say you have all of the elements of an array, but something is wrong, they’re all in the wrong order, they should be reverse. Well, array_reverse does just that, it will reverse the order of an array. Array_reverse will not maintain key association by default, pass a second argument of true to maintain key association.
usage:

$arrayVar=array(1, 28, 73, 17, 54);
 
print_r(array_reverse($arrayVar));
/*  Above code will return an array of
array
(
[0] => 54
[1] => 17
[2] => 73
[3] => 28
[4] => 1
)
*/

asort
PHP version 4 and up

Just as array_reverse asort allows you to sort an array, but this time choosing the sort method
usage:

$arrayVar=array('john', 'tom', 'ken');
 
print_r(asort($arrayVar));
/*  Above code will return an array of
array
(
[0] => john
[1] => ken
[2] => tom
)
*/

You can pass a second argument to affect the sorting. Valid sorting values are:
SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_LOCALE_STRING

array_search
PHP version 4.0.5 and up

Say you have an array, you want to know if a value exists in that array, and if so, what the key is to that value. For this you can use array_search, you feed this the value to search for and your array to search, it will return the key if found otherwise will return false.
usage:

$arrayVar=array(1, 28, 73, 17, 54);
 
// Will return 'found'
if(array_search(54,$arrayVar)!==false)
{
echo 'found';
}
else
{
echo 'not found';
}

array_unique
PHP version 4.0.1 and up

If you have an array of values that you need to have no duplicates, run them through array_unique. This function will return an array with any duplicates filtered out.
usage:

$arrayVar=array(1, 1, 73, 1, 54);
 
print_r(array_unique($arrayVar));
/*
above code will output
 
array
(
[0] => 1
[1] => 73
[2] => 54
}
*/

You can also feed the function a second argument to specify a sorting order for the end result. The values accepted for the second argument are:
SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_LOCALE_STRING

array_walk
PHP version 4 and up

Say you have an array where you want to manipulate all values in the same way. For this you could loop through them yourself and store the results, or you can use the array_walk function which is more efficiently designed for such a purpose.
usage:

function funcExample($value, $key)
{
echo $key.' is '.$value.' years old';
}
 
$arrayVar=array('john' => 35, 'tom' => 22, 'ken' => 73);
array_walk($arrayVar, "funcExample");
 
/* the above code will output
john is 35 years old
tom is 22 years old
ken is 73 years old
*/

count
PHP version 4 and up

You can use count to give you the total number of elements in an array. Keep in mind that this will not provide the numeric key/index for a numeric key/index array. Assuming the array is sorted numerically by key, the key would be count()-1 as a key count starts at 0.
usage:

$arrayVar=array(0 => 35, 1 => 22, 2 => 73);
//will echo 3
echo count($arrayVar);

in_array
PHP version 4 and up

Similar to array_search, in_array will search for a value within an array. The difference is that in_array returns a boolean true or false upon success or failure, respectively.
usage:

$arrayVar=array(0 => 35, 1 => 22, 2 => 73);
// will 'return true'
if(in_array(35, $arrayVar))
{
echo 'returned true';
}

shuffle
PHP version 4 and up

Shuffle will (pseudo) randomly reorder elements in an array
usage:

$arrayVar=array(35,22,73);
 
print_r(shuffle($arrayVar));
 
/* output will be an array randomly shuffled
an example output may be:
 
array
(
[0] => 22
[1] => 73
[2] => 35
)
*/

Take note that key associations are lost during shuffling. Which makes sense as you’re shuffling to randomize output, thus you’d have no need to directly access an element.

Conclusion

Hopefully you’ve found this useful. This list is not meant to be exhaustive and is only meant to be a reference for the array functions one is most likely to use.

Leave a Comment

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