| Start |
|---|
| Filtering 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 Filtering non-empty valuesThe simplest case of filtering is to remove all "empty" values: Filtering by callbackThis time we define our own filtering rule. Suppose we want to get only even numbers: The Filtering by indexA third parameter can be provided to the Indexes in filtered arrayNote that This happens because the values which were on positions 1 (there was If you need to loop through the result of a filter on an indexed array, you should first call |
| Removing elements from an array To remove an element inside an array, e.g. the element with the index 1. This will remove the apples from the list, but notice that For associative array you can remove like this: Now $fruit is Note that unsets the variable and thus removes the whole array, meaning none of its elements are accessible anymore. Removing terminal elementsarray_shift() (opens new window) - Shift an element off the beginning of array. Example: Output: array_pop() (opens new window) - Pop the element off the end of array. Example: Output: |
| Sorting an Array There are several sort functions for arrays in php: sort()Sort an array in ascending order by value. results in rsort()Sort an array in descending order by value. results in asort()Sort an array in ascending order by value and preserve the indecies. results in arsort()Sort an array in descending order by value and preserve the indecies. results in ksort()Sort an array in ascending order by key results in krsort()Sort an array in descending order by key. results in natsort()Sort an array in a way a human being would do (natural order). results in natcasesort()Sort an array in a way a human being would do (natural order), but case intensive results in shuffle()Shuffles an array (sorted randomly). 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. results in uasort()Sort an array with a user defined comparison function and preserve the keys. results in uksort()Sort an array by keys with a user defined comparison function. results in |
| 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()`** 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. Taken from the PHP documentation (opens new window) for If you'd like to achieve this, all you need to do is the following: This will now add This returns an array in the following order: Since Example: Output ($joinedArray): |
| 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 If the Since PHP 5.6 you can use Using |
| Exchange values with keys
|
Merge two arrays into one array Associative array:
|
Read Article
https://develop.horje.com/learn/1434/reference