Php array combine
From w3cyberlearnings
Contents |
PHP function array_combine
This function makes a new array by combining two arrays together. The first array will be the key, and the second array will be the value.
Syntax array_combine
- array1: array key
- array2: array value
array_combine(array1, array2);
Example 1
<?php $countries = array('China', 'USA', 'Cambodia', 'Thailand', 'UK'); $score = array(98, 98, 99, 99, 98); $country_score = array_combine($countries, $score); print_r($country_score); ?>
Output
Array ( [China] => 98 [USA] => 98 [Cambodia] => 99 [Thailand] => 99 [UK] => 98 )
Example 2
<?php $countries = array('China', 'USA', 'Cambodia', 'Thailand', 'UK'); $score = array(98, 98, 99, 99, 98); $country_score = array_combine($countries, $score); foreach ($countries as $c) { echo "{$c} scores {$country_score[$c]} <br/>"; } ?>
Output
China scores 98 USA scores 98 Cambodia scores 99 Thailand scores 99 UK scores 98
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