Repository
https://github.com/php/php-src
What Will I Learn?
I will learn the array methods ( Array_Fill_Keys,Array_Fill, Array_Flip and Array_Filter ) with examples.
- What's the array_fill_keys and how to use it.
- How to fill an array using the array_fill method.
- How to change between keys and values using array_flip method.
- How to filter an arrray by the array_filter method.
Requirements
- Server support PHP , Xampp or Wamp for example
- An IDE like Sublime text.
- Browser (Chrome for example)
Difficulty
- Basic
Description
In this tutorial we will continue our series about the PHP programming language, we will talk about the arrays methods the fifth part (Array_Fill_Keys,Array_Fill, Array_Flip and Array_Filter), let's start ..
1- Array_Fill_Keys :
The array_fill_keys method fills the array with the value and the values of the array passed as parameter as keys.
It has two parameters , " the value " which is the value that you will make in all the keys , and " the array " which is an array of values will be used as keys. Turns values that are not allowed as keys to text.
This method will return an associative array with the keys passed as parameter and the same value for them.
To use the array_fill_keys method we pass the keys and the value as parameters
$newArray = array_fill_keys($keys,$value);
I will use the array_fill_keys to fill an array of persons using an array of keys.
echo "<h3>Array Fill Keys </h3>";
$keys = array("Alexendre", "John", "Mick");
$persons = array_fill_keys($keys, "Alex Company");
echo "<pre>";
print_r($keys);
print_r($persons);
So "Alexendre, John and Mick" are employees in the Alex company and I used array_fill_keys to use them as keys and this is the result
2- Array_Fill :
The array_fill method fills a number of array elements equal to num with a value starting with the start_index.
It has 3 parameters , " the num " which is the number of elements that you want to fill it must be greater than 0, and the second parameter is " the value " that the method will fill , the third parameter is " the start index " is the index that method will start with , if it's a negative number it will start from the number and the next index will be 0.
This method will return an array with values passed as parameter , it returns an E-WARNING error if the number less then 0.
To use the array_fill method we pass the start_index, the num and the values as parameters
$newArray = array_fill($start,$num,$values);
I will fill an array using array_fill method , I have an array of persons contains 3 persons
$persons1 = array("person1", "person2", "person3");
$testArray = array_fill(0, 1, $persons1);
echo "<pre>";
print_r($testArray);
This method allows us to control the indexes and the numbers of the values and this is the result
3- Array_Flip :
The array_flip () method returns the array upside down; turns the array keys to array values and values into keys.
Note that the array values must be valid as keys, that is, integers or text. The function will issue a warning if the value is not correct, and the function will not include the warning in the returned array.
It has " the array " as parameter and it returns the flipped array if it succeeds and NULL if it fails.
To use the array_flip method we pass the array as parameter
$newArray = array_flip($array);
I want to create an array of languages , each language has a key, by using array_flip I will change the languages to be keys and the keys to be languages
$languages = array(
"en" => "English",
"fr" => "French",
"es" => "Spanish"
);
$languagesF = array_flip($languages);
echo "<h5>Normal Array</h5>";
echo "<pre>";
print_r($languages);
echo "</pre>";
echo "<h5>After Using The Array_Flip</h5>";
echo "<pre>";
print_r($languagesF);
The array_flip will take the keys "en , fr , es " and change them to values, also will take "English, French, Spanish " and change them to keys, this is the result
4- Array-Filter :
The array_filter method passes on each element of the array and passes it as an argument to the callback function. If the callback function returns true, the current element will enter from the array to the result array. The function maintains the keys associated with the values.
It has 2 parameters , the array and the callback function which is The function specified by the user to filter the array elements.
If you do not pass any function, all elements of the array equal to the false value will be deleted.
To use the array_filter method we pass the array as parameter
$newArray = array_filter($array);
I will create an array contains the days and the earnings in each day, for Tuesday we haven't worked the day is null
$review = array(
"Saturday" => "300$",
"Sunday" => 0,
"Manday" => "500$",
"Tuesday" => Null,
"Wednesday" => 0,
"Thursday" => "900$",
"Friday" => "300$"
);
$finalReview = array_filter($review);
echo "<h5>Normal Array</h5>";
echo "<pre>";
print_r($review);
echo "</pre>";
echo "<h5>After Using The Array_Filter</h5>";
echo "<pre>";
print_r($finalReview);
I have two days with the value 0 and one day with value Null , the array_filter will change the boolean value of this values to false and they will be deleted from the returned array
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)
Proof of Work Done
https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/fill.php