0

I have the following three arrays of objects:

First Array: $array1

Array (
    [0] => Array (
            [id] => 1
            [name] => world
           )
)

Second Array: $array2

Array (
    [count] => 1
)

Third Array: $array3

Array (
    [KM] => 2
)

I want to add the associative elements from $array2 and $array3 into the subarray at $array1[0].

Desired output:

Array (
    [0] => Array
        (
            [id] => 1
            [name] => world
            [count] => 1
            [KM] => 2
        )
)
8
  • is array_merge() not working for some reason? Commented Aug 7, 2014 at 18:49
  • php.net/manual/en/function.array-merge.php should do exactly what you want Commented Aug 7, 2014 at 18:49
  • @Don'tPanic no it is not working for me Commented Aug 7, 2014 at 18:50
  • I think I can guess why, but we would need to see how you are trying to use it in order to know for sure. Commented Aug 7, 2014 at 18:52
  • Array merge is not working for me Commented Aug 7, 2014 at 18:52

5 Answers 5

2

This is how you would use array_merge()

<?php
    $array1 = array(array("id" => 1, "name" => "world")); 
    $array2 = array("KM" => 2); 
    $array3 = array("count" => 1); 

    print_r(array(array_merge($array1[0], $array2, $array3))); 
?>

Which would output:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => world
            [KM] => 2
            [count] => 1
        )

)
Sign up to request clarification or add additional context in comments.

Comments

2

array_merge is the function you've been looking for

Sample code:

$output_array[0] = array_merge($array1[0], $array2, $array3));
print_r($output_array);

2 Comments

Direct array merge will not produce desired output.
$array1[0] is another array, please check the updated answer(with a demo code) @Rolice
1

This will get you the desired output:

<?php

$array1 = array(array("id" => 1, "name" => "world"));
$array2 = array("KM" => 2); 
$array3 = array("count" => 1); 

$array1[0] = array_merge($array1[0], $array2, $array3);
print_r($array1);
?>

Comments

0

array_merge() will merge arrays. Problem is, the structure of your $array1 differs from the others. You have an array inside an array. To produce the desired output as specified you would need this:

$array4 = array(
    0 => array_merge($array1[0], $array2, $array3)
);

But I believe you want something more like this:

$array4 = array_merge($array1[0], $array2, $array3);

Comments

0

array array_merge ( array $array1 [, array $... ] ) Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.

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.