php从两个数组中排出一个数组

时间:2022-01-19 12:12:36

I have a two arrays:

我有两个数组:

$ids:

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

$ranks:

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

How can I create an array from these two, like this array('1'=>'Rank1','2'=>'Rank2','3'=>'Rank3','4'=>'Rank5','8'=>'Noname')

如何从这两个数组创建一个数组,比如这个数组('1'=>'Rank1','2'=>'Rank2','3'=>'Rank3','4'=>'Rank5', '8'=> 'NONAME')

1 个解决方案

#1


10  

Use the array_combine() function, which allows you to map an array of keys to an array of values in one associative array.

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

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

Output:

输出:

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

#1


10  

Use the array_combine() function, which allows you to map an array of keys to an array of values in one associative array.

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

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

Output:

输出:

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