Jump to: navigation, search

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


Navigation
Web
SQL
MISC
References