PHP Tutorial #07 Array Methods ( Array_Keys, Array_Map, Array_Merge and Array_Pad )
Repository
https://github.com/php/php-src
What Will I Learn?
I will learn the array methods (array_keys, array_map, array_merge and array_pad) with examples.
- How to get keys with array_keys method.
- What's the array_map method and how to use it .
- How to merge between arrays using array_merge method.
- What's the array_pad method and how to use it.
Requirements
- Server support PHP , Xampp or Wamp for example
- An IDE like Sublime text.
- Browser (Chrome for example)
Difficulty
- Basic
Description
1 - Array_Keys
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
2 – Array_Map
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
3 – Array-Merge
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
4 – Array Pad
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 "
Video Tutorial
Curriculum
- PHP Tutorial #01 Indexed Arrays, Associative Arrays and Multidimensional Arrays
- PHP Tutorial #02 Array Methods (Sort, Rsort, Ksort, Krsort , Array_Reverse And Shuffle)
- PHP Tutorial #03 Array Methods ( Search Methods , Addition Methods and the Remove Methods)
- PHP Tutorial #04 Array Methods (Array_Sum, Array_Rand, Array_Column and Array_Unique)
- PHP Tutorial #05 Array Methods (Array_Chunk, Array_Combine, Array_Count_Values and Array_Product)
- PHP Tutorial #06 Array Methods (Array_Fill_Keys,Array_Fill, Array_Flip and Array_Filter)
Proof of Work Done
https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/keys_map.php
I thank you for your contribution. Here is my thought:
Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.
To view those questions and the relevant answers related to your post, click here.
Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]
Thank you @yokunjon, I will try to organize the tutorial more .
Thank you for your review, @yokunjon!
So far this week you've reviewed 1 contributions. Keep up the good work!
Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!
![](https://steemitimages.com/640x0/https://codingforspeed.com/images/i-love-coding.jpg)
Reply !stop to disable the comment. Thanks!
Thank you
Hey, @alexendre-maxim!
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!
Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).
Want to chat? Join us on Discord https://discord.gg/h52nFrV.
Vote for Utopian Witness!
Thank's