user1773168
user1773168

Reputation: 15

How to combine 2 arrays into 1 array

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

Upvotes: 0

Views: 170

Answers (3)

Florian Kasper
Florian Kasper

Reputation: 496

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
)

Upvotes: 0

deceze
deceze

Reputation: 522636

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

Upvotes: 1

air4x
air4x

Reputation: 5683

Try

$array3 = array();
foreach ($array1 as $key => $value) {
  $array3[$value['id']] = $array2[$key];
}

Upvotes: 4

Related Questions