Martin Lansik
Martin Lansik

Reputation: 23

How to merge these 2 arrays in PHP?

I have an array $array1 with different amount of key and value pairs:

Array
(
    [0] => Array
        (
            [ID] => 39
            [title] => Apple
        )

    [1] => Array
        (
            [ID] => 40
            [title] => Orange
        )

)

and another array $array2:

Array
(
    [0] => 273
    [1] => 386

)

And I want to get this:

Array
(
    [0] => Array
        (
            [ID] => 39
            [title] => Apple
            [pages] => 273
        )

    [1] => Array
        (
            [ID] => 40
            [title] => Orange
            [pages] => 386
        )

)

The number of items in each array is the same and the correspond, so, we don't need to check this, so, how to merge it like that?

Upvotes: 1

Views: 90

Answers (7)

Sunny S.M
Sunny S.M

Reputation: 5978

Here is code that produce exactly output you are looking for :

<?php
    $list1 = array(array('id' => 39,'title' => 'Apple'),array('id' => 40,'title' => 'Orange'));
    $list2 = array(array('pages' => 273,'year' => 1981),array('pages' => 386,'year' => 1979));

    $newList = array();

    $i=0;
    foreach($list1 as $firstList){
        foreach($list2 as $secondList){
            $newList[$i] = array_merge($firstList, $secondList);
        }
    $i++;
    }

    echo"<pre>"; print_r($newList); echo"</pre>";

?>

OUTPUT :

enter image description here

Upvotes: 1

memo
memo

Reputation: 273

If the sizes of the 2 arrays are same you could do something like this

$arr1 = array(
    array(
        'ID' => 39,
        'title' => 'Apple'
    ),
    array(
        'ID' => 40,
        'title' => 'Orange'
    )
);

$arr2 = array(
    273,
    386
);

$merged = array();

for ($i = 0; $i < count($arr1); $i++) {
    $merged[] = array_merge($arr1[$i], array('pages' => $arr2[$i]));
}

var_dump($merged);

or if you don't want a new array

for ($i = 0; $i < count($arr1); $i++) {
    $arr1[$i]['pages'] = $arr2[$i];
}

Upvotes: 0

Richard Merchant
Richard Merchant

Reputation: 1003

$output = array();
array_walk( $array1, function( $v, $k ) use ( $array2, &$output ) {
       $output[] = array_merge( $v, $array2[$k] );
    });

Upvotes: 0

Atan
Atan

Reputation: 1113

for($i=0; $i<count($array1); ++$i){
    $array1[$i]['pages'] = $array2[$i];
}
var_dump($array1);

Upvotes: 0

Anton Ohorodnyk
Anton Ohorodnyk

Reputation: 883

use array_replace_recursive if you want merge with integer keys, or array_merge_recursive if you want merge only string keys

<?php

$a1 = array(
    0 => array
    (
        "ID" => 39,
        "title" => "Apple"
    ),

    1 => array(
        "ID" => 40,
        "title" => "Orange"
    )

);

$a2 = array(
    0 => array
    (
        "pages" => 273,
        "year" => 1981
    ),

    1 => array(
        "pages" => 386,
        "year" => 1979
    )

);

$a3 = array_replace_recursive($a1, $a2);

var_dump($a3);

Result:

array(2) {
  [0] =>
  array(4) {
    'ID' =>
    int(39)
    'title' =>
    string(5) "Apple"
    'pages' =>
    int(273)
    'year' =>
    int(1981)
  }
  [1] =>
  array(4) {
    'ID' =>
    int(40)
    'title' =>
    string(6) "Orange"
    'pages' =>
    int(386)
    'year' =>
    int(1979)
  }
}

Answer for updated question:

<?php

$a1 = array(
    0 => array
    (
        "ID" => 39,
        "title" => "Apple"
    ),

    1 => array(
        "ID" => 40,
        "title" => "Orange"
    )

);

$a2 = array(
    0 => 31,
    1 => 324
);

$defaultValue = 0;
foreach ($a1 as $key => $value) {
    $a1[$key]['pages'] = isset($a2[$key]) ? $a2[$key] : $defaultValue;
}
var_dump($a1);

Result:

array(2) {
  [0] =>
  array(3) {
    'ID' =>
    int(39)
    'title' =>
    string(5) "Apple"
    'pages' =>
    int(31)
  }
  [1] =>
  array(3) {
    'ID' =>
    int(40)
    'title' =>
    string(6) "Orange"
    'pages' =>
    int(324)
  }
}

Upvotes: 7

Sugumar Venkatesan
Sugumar Venkatesan

Reputation: 4028

Try this

array1[0][pages]=array2[0][pages];
array1[0][year]=array2[0][year];
array1[1][pages]=array2[1][pages];
array1[1][year]=array2[1][year];

Upvotes: 0

Abhishek Sharma
Abhishek Sharma

Reputation: 6661

Try this code :-

$newArray = array();
$i=0;
foreach($array1 as $value){
  $newArray[$i] = $value;
  $newArray[$i][] = $array2[$i];
  $i++;
}

Upvotes: 1

Related Questions