0

I have 2 arrays like that:

array1
      (
        [0] => Array
                   (
                     [id] => 133
                   )

        [1] => Array
                   (
                     [id] => 134
                   )

      )

array2
      (
        [0] => 1
        [1] => 2

      )

My problem is: how can I combine two arrays into one array like:

array3
      (
        [133] => 1
        [134] => 2

      )

Thanks for any help :D

0

3 Answers 3

4

Try

$array3 = array();
foreach ($array1 as $key => $value) {
  $array3[$value['id']] = $array2[$key];
}
Sign up to request clarification or add additional context in comments.

Comments

1
$array3 = array_combine(array_map('current', $array1), $array2);

3 Comments

What is "current" in the map?
It takes the current (first) element from the child array, in essence flattening the array.
Nice tip deceze. This is a much tidier solution.
0

I've done it like this:

<?php
    $arrayOne = array(
        array("id" => 133),
        array("id" => 134)
    );
    $arrayTwo = array(1,2);
    $arrayThree = array();
    foreach($arrayOne as $index => $value){
        $arrayThree[$value['id']] = $arrayTwo[$index];
    }

if you do a

print_r($arrayThree);

now you will get your third array:

Array
(
    [133] => 1
    [134] => 2
)

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.