从键数组和值数组创建关联数组

问题描述 投票:0回答:1

我有两个数组:

$ids = Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 8
)

$ranks = Array
(
    [0] => Rank1
    [1] => Rank2
    [2] => Rank3
    [3] => Rank5
    [4] => NoName
)

如何从这两个数组创建一个数组,如下所示:

array(
    '1'=>'Rank1',
    '2'=>'Rank2',
    '3'=>'Rank3',
    '4'=>'Rank5',
    '8'=>'Noname'
)
php arrays associative-array
1个回答
10
投票

使用

array_combine()
函数,它允许您将一组键映射到一个关联数组中的一组值。

$ids_ranks = array_combine($ids, $ranks);
print_r($ids_ranks);

输出:

Array
(
    [1] => Rank1
    [2] => Rank2
    [3] => Rank3
    [4] => Rank5
    [8] => NoName
)
© www.soinside.com 2019 - 2024. All rights reserved.