0

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 Answer 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.

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

Output:

Array
(
    [1] => Rank1
    [2] => Rank2
    [3] => Rank3
    [4] => Rank5
    [8] => NoName
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.