1

1st Array: One Dimension, 2nd Array: Two Dimension

I want to merge the two arrays by key, keep the keys and the values of the 2nd Array

1st Array (
[30] => 30
[28] => 28
[27] => 27
[16] => 16
)

2nd Array (
[27] => Array (
        [person_id] => 27
        [person_name] => Jazz Club
        [person_job] => 10
        [drink_price] => 5
       )

)

Expected result Array (
[30] => 30
[28] => 28
[27] => [27] => Array
       (
        [person_id] => 27
        [person_name] => Jazz Club
        [person_job] => 10
        [drink_price] => 5
       )
[16] => 16
)
3
  • do you mean [27] => [27] => Array ( [person_id] => 27... or this [27] => Array ( [person_id] => 27... please check your question. Commented Sep 14, 2013 at 23:40
  • Actually I just found the solution of @Radek here, which works! --> how to merge multidimensional arrays whilst preserving all unique key/values? Commented Sep 15, 2013 at 0:27
  • @user2119165, that would work but I would say that $newarr = $arr2 + $arr1 is much easier since support for doing this kind of operation is built into PHP itself. Give it a try :) Commented Sep 16, 2013 at 11:27

2 Answers 2

3

See this

If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the + array union operator:

<?php
$array1 = array(0 => 'zero_a', 2 => 'two_a', 3 => 'three_a');
$array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b');
$result = $array1 + $array2;
var_dump($result);
?>

The keys from the first array will be preserved. If an array key exists in both arrays, then the element from the first array will be used and the matching key's element from the second array will be ignored.

array(5) {
  [0]=>
  string(6) "zero_a"
  [2]=>
  string(5) "two_a"
  [3]=>
  string(7) "three_a"
  [1]=>
  string(5) "one_b"
  [4]=>
  string(6) "four_b"
}

Your solution should be:

<?
$arr1 = array (
 30=> 30,
 28=> 28,
 27=> 27,
 16=> 16
);

$arr2 = array (
27 => array (
        person_id => 27,
        person_name => "Jazz Club",
        person_job => 10,
        drink_price => 5
       )

);

$newarr = $arr2 + $arr1;

print_r($newarr);
?>

Gives the following result

Array
(
    [27] => Array
        (
            [person_id] => 27
            [person_name] => Jazz Club
            [person_job] => 10
            [drink_price] => 5
        )

    [30] => 30
    [28] => 28
    [16] => 16
)
Sign up to request clarification or add additional context in comments.

5 Comments

would this give the expected output?
As I understood from what you said that would not give the expected result please check the question.
Yes I think this is what the OP wants
so what you say about this? [27] => [27] => Array (
I think the text of the question and the code in the question are different. I believe the text in the question speaks for what the OP wants to achieve.
0

You can achieve this by this simple line :

$firstArray[27]=$2ndArray;

DEMO HERE

Example :

<?php
$first[14]=14;
$first[16]=16;

$first[17]=17;
$first[21]=21;

$second[27]=Array ('person_id' => 27
        ,'person_name' => 'Jazz Club'
        ,'person_job' => 10
        ,'drink_price' => 5
       );

$first[27]=$second;

echo '<pre>';

print_r($first);
echo '</pre>';

OUTPUT:

Array
(
    [14] => 14
    [16] => 16
    [17] => 17
    [21] => 21
    [27] => Array
        (
            [27] => Array
                (
                    [person_id] => 27
                    [person_name] => Jazz Club
                    [person_job] => 10
                    [drink_price] => 5
                )

        )

)

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.