合并键匹配的两个数组

时间:2011-03-03 02:27:57

标签: php arrays

我有两个匹配键的数组,我需要将两者的值合并到一个新数组中。我正在努力解决这个问题。我怎么能这样做?

$options = array(
    "0" => true,
    "1" => true,
    "2" => false,
    "3" => true
);

$columns = array(
    "0" => "first",
    "1" => "last",
    "2" => "id",
    "3" => "group"
);

$what_I_need = array(
    "first" => true,
    "last" => true,
    "id" => false,
    "group" => true
);

3 个答案:

答案 0 :(得分:7)

$whatYouNeed = array_combine($columns, $options);

假设数组的顺序正确。否则:

$whatYouNeed = array();
foreach ($columns as $key => column) {
    $whatYouNeed[$column] = $options[$key];
}

答案 1 :(得分:2)

$what_I_need = array_combine($columns , $options);

array_combine

答案 2 :(得分:2)

array array_combine ( array $keys , array $values );

$newArray = array_combine($columns, $options);
print_r($newArray);

应该给你:

array(
    "first" => true,
    "last" => true,
    "id" => false,
    "group" => true
);

PHP array_combine