https://github.com/php/php-src
I will learn the array methods ( Array_Replace, Array_Values, Array_Slice and Array_Intersect ) with examples.
The array_replace method replaces the values of array1 with values that have the same keys in each of the following arrays. If there is a key from the first array in the second array, its value is replaced by the corresponding key value of the second array. If the key is in the second array, and not in the first, it will be created in the first array. If a key exists only in the first array, it will stay as it is. If multiple replacement arrays are passed, they will be processed in order, and subsequent arrays overwrite the previous values.
The array_replace method is not recursive : The values in the first array will be replaced by any type in the second array. It has at least two arrays as parameters , the array in which the elements will be replaced. And the array from which the elements are extracted.
The method returns an array, or the NULL value, if an error occurs.
To use the array_replace method we need to pass the arrays as parameter
array_replace($array1, $array2);
I have an array of persons and I want to replace some persons using another array
$team = array("Person1" => "Alexendre", "Person2" => "Alex", "Person3" => "John", "Person4" => "Arden", "Person5" => "Mick");
$replacedTeam = array("Person1" => "Mickel", "Person5" => "Gabriel");
print_r(array_replace($team, $replacedTeam));
I have used " array_replace " and I passed the $team and $replacedTeam arrays as parameters , the method will replace the values of keys exist in the first array by the values of the second array and this is the result
The array_values function returns all values of the array elements and recalculates the array numerically.
It has the array as parameter and it returns an array of values.
To use the array_values method we need to pass an array as parameter
array_values($array);
I have an array of languages and I want to get just the values of this array
$languages = array("en" => "English", "fr" => "French", "sp" => "Spanish"); print_r(array_values($languages));
The array_values will return an array of values indexed from 0 to the last element
The array_slice method returns a set of successive elements of the array as specified by the offset and length parameters. It has 4 parameters the array is the first parameter, the second parameter is the offset. If the offset coefficient is not negative, the relay will start from the beginning of the array. If offset is negative, the relay will start from the end of the array.
The third parameter is the length If the length coefficient is positive, the number of consecutive elements is equal. If the array is shorter than length, only the available array elements will be returned. If the length is negative, the sequence will stop at this number of elements descending from the end of the array. If you do not specify the value of this optional parameter, the sequence will be made up of all elements from offset to the end of the array.
The fourth parameter is the perverse key the array_slice will reorder and set the numeric array pointers by default. But we can change this behavior by setting the value of the reserve_keys parameter to TRUE. The method returns a portion of the array. If the offset coefficient is greater than the size of the array, the function returns an empty array.
To use the array_slice we need to pass the array , the start index , the length and the optional parameter which is the preverse key
array_slice($array,$start,$length,$preverse);
I have an array of friends and I want to use array_slice method to get a special elements in the array
$teamS = array( "Alexendre", "Alex", "John", "Arden", "Mick");
print_r($teamS);
print_r(array_slice($teamS, 1, 3));
I passed the $teamS array as parameter , the start index is 1 it will start from the second element and the length that will be is 3 , this is the result
The function returns all array1 values in all other arrays. Note that the function preserves the keys associated with the returned values. It has at least two arrays as parameters and it return an array.
To use the array_intersect method we need to pass at least to parameters
array_intersect($array1,$array2);
I have two arrays group1 and group2 each array contains number of persons, I want to get the persons exist in the first and second arrays
$group1 = array("Person1", "Person2", "Person3", "Person4", "Person5");
$group2 = array("Person1", "Person3", "person6");
print_r(array_intersect($group1, $group2));
The array_intersect will return an array contains the elements exist in group1 and group2, it will return person1 and person3 because they are in the two arrays , this is the result
https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/replace.php