Php array diff key
From w3cyberlearnings
Contents |
PHP function array_diff_key
This function compares two or more arrays, and the comparison is based on the first array and it uses array key for the comparison. It returns the array element in the first array that is not found in other arrays.
Syntax array_diff_key
- array1: array that other arrays used for the comparsion
- array2: second array
- array3: third array
array_diff_key(array1, array2, array3, ...);
Example 1
<?php $score1 = array(50 => 'China', 49 => 'US', 43 => 'Vietnam', 44 => 'Mexico'); $score2 = array(50 => 'Mexico', 30 => 'Canana', 43 => 'China', 43 => 'US'); $aa_diff = array_diff_key($score1, $score2); print_r($aa_diff); ?>
Output
Array ( [49] => US [44] => Mexico )
Example 2
<?php $score1 = array(50 => 'China', 49 => 'US', 43 => 'Vietnam', 44 => 'Mexico'); $score2 = array(50 => 'Mexico', 30 => 'Canana', 43 => 'China', 43 => 'US'); $aa_diff = array_diff_key($score1, $score2); foreach ($aa_diff as $score => $country) { echo "The second team did not score {$score} as the players from {$country}<br/>"; } ?>
Output
The second team did not score 49 as the players from US The second team did not score 44 as the players from Mexico
Related Links
- array_change_key_case
- array_chunk
- array_combine
- array_count_values
- array_diff_assoc
- array_diff_key
- array_diff_uassoc
- array_diff_ukey
- array_diff
- array_fill_keys
- array_fill
- array_filter
- array_flip
- array_intersect_assoc
- array_intersect_key
- array_intersect_uassoc
- array_intersect_ukey
- array_intersect
- array_key_ exists
- array_keys