- # Manipulating an Array
- # Filtering non-empty values
- # Filtering by callback
- # Filtering by index
- # Indexes in filtered array
- # Removing elements from an array
- # Removing terminal elements
- # Sorting an Array
- # sort()
- # rsort()
- # asort()
- # arsort()
- # ksort()
- # krsort()
- # natsort()
- # natcasesort()
- # shuffle()
- # usort()
- # uasort()
- # uksort()
- # Adding element to start of array
- # Whitelist only some array keys
- # Exchange values with keys
- # Merge two arrays into one array
- array_filter(): Filtering Arrays
- Remove empty array elements
- Filter arrays by indexed key
- Filter arrays by keys and values
# Manipulating an Array
In order to filter out values from an array and obtain a new array containing all the values that satisfy the filter condition, you can use the array_filter function.
# Filtering non-empty values
The simplest case of filtering is to remove all «empty» values:
$my_array = [1,0,2,null,3,'',4,[],5,6,7,8]; $non_empties = array_filter($my_array); // $non_empties will contain [1,2,3,4,5,6,7,8];
# Filtering by callback
This time we define our own filtering rule. Suppose we want to get only even numbers:
$my_array = [1,2,3,4,5,6,7,8]; $even_numbers = array_filter($my_array, function($number) return $number % 2 === 0; >);
The array_filter function receives the array to be filtered as its first argument, and a callback defining the filter predicate as its second.
# Filtering by index
A third parameter can be provided to the array_filter function, which allows to tweak which values are passed to the callback. This parameter can be set to either ARRAY_FILTER_USE_KEY or ARRAY_FILTER_USE_BOTH , which will result in the callback receiving the key instead of the value for each element in the array, or both value and key as its arguments. For example, if you want to deal with indexes istead of values:
$numbers = [16,3,5,8,1,4,6]; $even_indexed_numbers = array_filter($numbers, function($index) return $index % 2 === 0; >, ARRAY_FILTER_USE_KEY);
# Indexes in filtered array
Note that array_filter preserves the original array keys. A common mistake would be to try an use for loop over the filtered array:
$my_array = [1,0,2,null,3,'',4,[],5,6,7,8]; $filtered = array_filter($my_array); error_reporting(E_ALL); // show all errors and notices // innocently looking "for" loop for ($i = 0; $i count($filtered); $i++) print $filtered[$i]; > /* Output: 1 Notice: Undefined offset: 1 2 Notice: Undefined offset: 3 3 Notice: Undefined offset: 5 4 Notice: Undefined offset: 7 */
This happens because the values which were on positions 1 (there was 0 ), 3 ( null ), 5 (empty string » ) and 7 (empty array [] ) were removed along with their corresponding index keys.
If you need to loop through the result of a filter on an indexed array, you should first call array_values on the result of array_filter in order to create a new array with the correct indexes:
$my_array = [1,0,2,null,3,'',4,[],5,6,7,8]; $filtered = array_filter($my_array); $iterable = array_values($filtered); error_reporting(E_ALL); // show all errors and notices for ($i = 0; $i count($iterable); $i++) print $iterable[$i]; > // No warnings!
# Removing elements from an array
To remove an element inside an array, e.g. the element with the index 1.
$fruit = array("bananas", "apples", "peaches"); unset($fruit[1]);
This will remove the apples from the list, but notice that unset does not change the indexes of the remaining elements. So $fruit now contains the indexes 0 and 2 .
For associative array you can remove like this:
$fruit = array('banana', 'one'=>'apple', 'peaches'); print_r($fruit); /* Array ( [0] => banana [one] => apple [1] => peaches ) */ unset($fruit['one']);
print_r($fruit); /* Array ( [0] => banana [1] => peaches ) */
unsets the variable and thus removes the whole array, meaning none of its elements are accessible anymore.
# Removing terminal elements
(opens new window) — Shift an element off the beginning of array.
$fruit = array("bananas", "apples", "peaches"); array_shift($fruit); print_r($fruit);
Array ( [0] => apples [1] => peaches )
(opens new window) — Pop the element off the end of array.
$fruit = array("bananas", "apples", "peaches"); array_pop($fruit); print_r($fruit);
Array ( [0] => bananas [1] => apples )
# Sorting an Array
There are several sort functions for arrays in php:
# sort()
Sort an array in ascending order by value.
$fruits = ['Zitrone', 'Orange', 'Banane', 'Apfel']; sort($fruits); print_r($fruits);
Array ( [0] => Apfel [1] => Banane [2] => Orange [3] => Zitrone )
# rsort()
Sort an array in descending order by value.
$fruits = ['Zitrone', 'Orange', 'Banane', 'Apfel']; rsort($fruits); print_r($fruits);
Array ( [0] => Zitrone [1] => Orange [2] => Banane [3] => Apfel )
# asort()
Sort an array in ascending order by value and preserve the indecies.
$fruits = [1 => 'lemon', 2 => 'orange', 3 => 'banana', 4 => 'apple']; asort($fruits); print_r($fruits);
Array ( [4] => apple [3] => banana [1] => lemon [2] => orange )
# arsort()
Sort an array in descending order by value and preserve the indecies.
$fruits = [1 => 'lemon', 2 => 'orange', 3 => 'banana', 4 => 'apple']; arsort($fruits); print_r($fruits);
Array ( [2] => orange [1] => lemon [3] => banana [4] => apple )
# ksort()
Sort an array in ascending order by key
$fruits = ['d'=>'lemon', 'a'=>'orange', 'b'=>'banana', 'c'=>'apple']; ksort($fruits); print_r($fruits);
Array ( [a] => orange [b] => banana [c] => apple [d] => lemon )
# krsort()
Sort an array in descending order by key.
$fruits = ['d'=>'lemon', 'a'=>'orange', 'b'=>'banana', 'c'=>'apple']; krsort($fruits); print_r($fruits);
Array ( [d] => lemon [c] => apple [b] => banana [a] => orange )
# natsort()
Sort an array in a way a human being would do (natural order).
$files = ['File8.stack', 'file77.stack', 'file7.stack', 'file13.stack', 'File2.stack']; natsort($files); print_r($files);
Array ( [4] => File2.stack [0] => File8.stack [2] => file7.stack [3] => file13.stack [1] => file77.stack )
# natcasesort()
Sort an array in a way a human being would do (natural order), but case intensive
$files = ['File8.stack', 'file77.stack', 'file7.stack', 'file13.stack', 'File2.stack']; natcasesort($files); print_r($files);
Array ( [4] => File2.stack [2] => file7.stack [0] => File8.stack [3] => file13.stack [1] => file77.stack )
# shuffle()
Shuffles an array (sorted randomly).
$array = ['aa', 'bb', 'cc']; shuffle($array); print_r($array);
As written in the description it is random so here only one example in what it can result
# usort()
Sort an array with a user defined comparison function.
function compare($a, $b) if ($a == $b) return 0; > return ($a $b) ? -1 : 1; > $array = [3, 2, 5, 6, 1]; usort($array, 'compare'); print_r($array);
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 5 [4] => 6 )
# uasort()
Sort an array with a user defined comparison function and preserve the keys.
function compare($a, $b) if ($a == $b) return 0; > return ($a $b) ? -1 : 1; > $array = ['a' => 1, 'b' => -3, 'c' => 5, 'd' => 3, 'e' => -5]; uasort($array, 'compare'); print_r($array);
Array ( [e] => -5 [b] => -3 [a] => 1 [d] => 3 [c] => 5 )
# uksort()
Sort an array by keys with a user defined comparison function.
function compare($a, $b) if ($a == $b) return 0; > return ($a $b) ? -1 : 1; > $array = ['ee' => 1, 'g' => -3, '4' => 5, 'k' => 3, 'oo' => -5]; uksort($array, 'compare'); print_r($array);
Array ( [ee] => 1 [g] => -3 [k] => 3 [oo] => -5 [4] => 5 )
# Adding element to start of array
Sometimes you want to add an element to the beginning of an array without modifying any of the current elements (order) within the array. Whenever this is the case, you can use array_unshift()
**`array_unshift()`** prepends passed elements to the front of the array. Note that the list of elements is prepended as a whole, so that the prepended elements stay in the same order. All numerical array keys will be modified to start counting from zero while literal keys won’t be touched.
If you’d like to achieve this, all you need to do is the following:
$myArray = array(1, 2, 3); array_unshift($myArray, 4);
This will now add 4 as the first element in your array. You can verify this by:
This returns an array in the following order: 4, 1, 2, 3 .
Since array_unshift forces the array to reset the key-value pairs as the new element let the following entries have the keys n+1 it is smarter to create a new array and append the existing array to the newly created array.
$myArray = array('apples', 'bananas', 'pears'); $myElement = array('oranges'); $joinedArray = $myElement; foreach ($myArray as $i) $joinedArray[] = $i; >
Array ( [0] => oranges [1] => apples [2] => bananas [3] => pears )
# Whitelist only some array keys
When you want to allow only certain keys in your arrays, especially when the array comes from request parameters, you can use array_intersect_key together with array_flip .
If the parameters variable doesn’t contain any allowed key, then the filteredParameters variable will consist of an empty array.
(opens new window) flag as the third parameter:
Using array_filter gives the additional flexibility of performing an arbitrary test against the key, e.g. $allowedKeys could contain regex patterns instead of plain strings. It also more explicitly states the intention of the code than array_intersect_key() combined with array_flip() .
# Exchange values with keys
array_flip function will exchange all keys with its elements.
$colors = array( 'one' => 'red', 'two' => 'blue', 'three' => 'yellow', ); array_flip($colors); //will output array( 'red' => 'one', 'blue' => 'two', 'yellow' => 'three' )
# Merge two arrays into one array
$a1 = array("red","green"); $a2 = array("blue","yellow"); print_r(array_merge($a1,$a2)); /* Array ( [0] => red [1] => green [2] => blue [3] => yellow ) */
$a1=array("a"=>"red","b"=>"green"); $a2=array("c"=>"blue","b"=>"yellow"); print_r(array_merge($a1,$a2)); /* Array ( [a] => red [b] => yellow [c] => blue ) */
- Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
- If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
- Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.
array_filter(): Filtering Arrays
The array_filter() function filters the elements of an array by applying a callback function to each element to decide whether it appears in the output array. The callback function returns true upon success and false otherwise.
The array_filter() function returns the filtered array and accepts three parameters:
- $array – an input array
- $callback – (optional) a function, if not supplied, all empty entries of the array will be removed.
- $mode – (optional) by default pass value as the only argument to the callback.
Use the ARRAY_FILTER_USE_KEY flag to pass key instead.
Use the ARRAY_FILTER_USE_BOTH flag to pass both value and key.
Remove empty array elements
PHP considers a value empty when converting to boolean type returns false. The following values are considered empty:
- FALSE
- 0 , ‘0’ , «0» , 0.0 , or -0.0
- Empty string » and «»
- Empty array (array with zero elements) array() or []
- NULL or undefined variables
When you use the array_filter function without a callback function it removes all the values which PHP considered as empty:
Example: Remove all empty values with array_filter()
Example: Remove empty strings from an array with array_filter()
return true; > var_dump($b); /* prints: array(6) < [0]=>string(1) "a" [2]=> NULL [3]=> string(1) "b" [5]=> string(1) "c" [6]=> int(0) [7]=> bool(false) >*/
Example: Validate a list of email addresses with array_filter()
$list = [ 'admin@brainbell.com', 'admin@brainbell_com', 'info[at]brainbell.com', 'info@brainbell.com' ]; $validList = array_filter($list, 'validateEmailAddress'); print_r($validList); /*Prints: Array ( [0] => admin@brainbell.com [3] => info@brainbell.com )*/
The array_filter() filter out the invalid addresses so that only (syntactically) valid email addresses are left. As you would expect, the code just prints out the two valid email addresses.
Note: The array_filter() function keeps the keys/values association intact. If you need to reindex the array after removing the empty elements, use: array_values() function.
admin@brainbell.com [1] => info@brainbell.com )*/
Filter arrays by indexed key
Use the ARRAY_FILTER_USE_KEY flag if you want to deal with array indexes instead of the values.
$a = ['a','b','c']; $f = array_filter($a, 'filterKey', ARRAY_FILTER_USE_KEY ); print_r($f); /* Array ( [0] => a [2] => c )*/
Filter arrays by keys and values
Use the ARRAY_FILTER_USE_BOTH flag if you want to deal with both values and keys.
$a = ['a','b','c']; $f = array_filter($a, 'filterBoth', ARRAY_FILTER_USE_BOTH ); print_r($f); /* Array ( [0] => a [2] => c )*/
Working with arrays: