https://github.com/php/php-src
I will learn the array methods (Array_Diff_Key, Array_Diff, Array_Intersect_Key and Array_Change_Key_Case ) with examples.
The method compares the passed arrays keys as parameters and returns the difference between them. This function is similar to the array_diff () function, but it is compared by using keys, not values.
It has at least two arrays as parameters and it returns a new array of values in the first array whose keys do not exist in any of the other arrays.
This method checks only one dimension of multidimensional arrays. Of course you can check deeper dimensions using square brackets.
To use the " array_diff_key " you must pass at least two arrays as parameters
array_diff_key($array1, $array2);
I have two arrays " Languages and ExistingLanguages " I want to print all languages exist in the world and not exist in my country
$languages = array(
"en" => "English",
"fr" => "French",
"es" => "Spanish"
);
$existingLanguages = array(
"fr" => "French"
);
print_r(array_diff_key($languages, $existingLanguages));
The array_diff_key will return an array containing all elements by key present in the first array and not present in the second array
The array_diff method compares the array with one or more other arrays and returns the values in them that are not in other arrays.
It has at least two arrays as parameters and it returns a new array containing all elements in the first array that are not present in any of the other arrays.
To use the " array_diff " you must pass at least two arrays as parameters
array_diff($array1, $array2);
I have a list of students and another list contains the successful students, I need the rest or the students who did not succeed
$students = array("Alexendre", "Alex", "Mick", "Arden", "John");
$studentsSucc = array("Alexendre", "Arden", "John");
echo "<h5>Students</h5>";
print_r($students);
echo "<h5>Succ Students</h5>";
print_r($studentsSucc);
echo "<h5>The Rest</h5>";
print_r(array_diff($students, $studentsSucc));
The array_diff will return all the elements by value exist in the first array and not in the second array , it means it will return the rest of students
The method returns all elements of the array whose keys are in all other arrays.
It has at least two arrays as parameters and it returns a linked array containing all elements of the existing array with all other arrays.
It's like the array_intersect method but it's interested in comparing keys.
To use the " array_intersect_key " you must pass at least two arrays as parameters
array_intersect_key($array1, $array2);
I want to get the languages exist in the world and in my country and for that I need to use the array_intersect_key method
echo "<h3>Array_Intersect_Key</h3>";
print_r(array_intersect_key($languages, $existingLanguages));
The languages array contains " English, French and Spanish " , and the existingLanguages contains just the " French " and this is the result
This method returns an array that contains all the array keys passed as a parameter to the function after converting its case to large or small. Note that numeric keys will remain unchanged as they are.
It has two parameters , the first parameters is the array The second parameters is the case which is the Latin case large CASE_UPPER or small CASE_LOWER .
This method returns an array containing keys whose case has been converted to a large or small case, or the false value if the parameter type is not an array. If the array is empty it twill return an E-WARNING error.
To use the array_change_key_case method we need to pass two parameters , the array and the choice between UPPER or lower
array_change_key_case($array, CASE_UPPER/LOWER);
I have an array and I want to change the case of keys to UPPER and LOWER
echo "<h3>Array_Change_Key_Case</h3>";
$flippedArray = array_flip($languages);
print_r($flippedArray);
echo "<h5>UPPER</h5>";
print_r(array_change_key_case($flippedArray, CASE_UPPER));
echo "<h5>LOWER</h5>";
print_r(array_change_key_case($flippedArray, CASE_LOWER));
I have used the array_flip to be more clear, and this is the result
https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/diff.php