https://github.com/php/php-src
I will learn the array methods (array_keys, array_map, array_merge and array_pad) with examples.
The method returns keys, whether they are integers or text strings. If the user selects a value for the optional search_value parameter, the method returns the keys associated with this value only. Or the function will return all the array keys except that.
It has 3 parameters " the array " that contains the keys that you want to get , " the search value " if you select this value , the method will return just the keys associated with this value , and " the strict " parameter which is a Boolean value if it's true the search will be true if the two values are equals.
This method will return an array contains the keys of the array.
To use the array_keys method we must pass the array as parameter, and if you want to specify the value, you can pass it also as parameter
array_keys($array, $value, $strict);
I have an array of languages contains the key of each language , I want to get the keys from this array
$languagesKeys = array_keys($languages, "French", true);
echo "<pre>";
print_r($languagesKeys);
I have used the array_keys method and the result will be in $languagesKeys array
The method returns all elements of the array after the callback function is executed on each element.
It has two parameters , the first is " the array " which is an array containing elements that the function will pass on , and the second is " the callback function " which is a function defined by the user to execute on each element of the array.
The function returns an array that contains new elements after the callback function is executed on each of them.
To use the array_map method we pass two parameters , the array and the callback function
array_map($array, $callback);
I will use the array_map to test the type of elements in the friends array
function testString($s){
if(gettype($s) === "string"){
return $s;
} else {
return "Unknown";
}
}
$correctFriends = array_map("testString", $friends); print_r($correctFriends);
I have a function to test the type of the item passed as parameter if it's string the function will return the item else will return " Unknown " and this is the result
The method merges one or more array elements so that it adds array elements to the elements of its array. The function returns the resulting array containing the elements of all arrays.
If the array contains identical keys in the text representation, the function will add items associated with the following keys to replace the previous values recorded with the key. If the array contains the same numerical key, the value of the associated element will not delete the value of the first element but will be added with it.
The function will return the numbering of the numeric keys in the arrays ascending from zero in the returned array.
It has the array as parameter and it return another array.
To use the array_merge we pass at least two arrays as parameters
array_merge($array1, $array2);
I will add other languages to the languages array using the array_merge method
$otherLanguages = array(
"dt" => "Dutch",
"fr" => "Germany"
);
$allLanguages = array_merge($languages, $otherLanguages);
print_r($allLanguages);
The array_merge will merge between the first and the second array, if it finds a key exists in the first and second array it will replace the value by the value of the second array
The array_pad method returns a copy of the array filled with items with the value to the specified size. If the size is positive, the array will be filled from the right, or if it is negative, it will be filled from the left. If the absolute value of size is less than or equal to the length of the array, there will be no increase in size. It is possible to add a maximum of 1048576 items at a time.
It has 3 parameters the array , the size and the value.
To use the array_pad method we pass the array, the length and the value
array_pad($array, $num, $value);
I have an array contains 3 elements and I want to fill my array
$products = array("Product1", "Product2", "Product3"); // 3
$newProducts = array_pad($products, 7, "Empty");
print_r($newProducts);
I have 3 elements and it needs 4 elements to get the size 7 , it will complete the number with the value " Empty "
https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/keys_map.php