Tutorial by Examples

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 $fr...
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,'',...
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 ...
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. $parameters = ['foo' => 'bar', 'bar' => 'baz', 'boo' => 'bam']; $allowedKeys = ['foo', 'bar']; $filteredParameters =...
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); results in Array ( [0] => Apfel [1] => Banane [2] => Orange [3] => Zitr...
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' )
$a1 = array("red","green"); $a2 = array("blue","yellow"); print_r(array_merge($a1,$a2)); /* Array ( [0] => red [1] => green [2] => blue [3] => yellow ) */ Associative array: $a1=array("a"=>"red","b"=&...

Page 1 of 1