Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've two arrays with ids as key and some fields and I would like to merge them but I don't understand why it doesn't work, here is my example code:

$Podcasts1 = array("1234" => array('title' => "myTitle", "type" => "myType"));
$Podcast2 = array("1234" => array("content" => "myContent"));
$podcasts = array_merge_recursive($Podcasts1, $Podcast2);
var_dump($Podcasts1, $Podcast2, $podcasts);

There is the result:

array:1 [▼
  1234 => array:2 [▼
    "title" => "myTitle"
    "type" => "myType"
  ]
]
array:1 [▼
  1234 => array:1 [▼
    "content" => "myContent"
  ]
]
array:2 [▼
  0 => array:2 [▼
    "title" => "myTitle"
    "type" => "myType"
  ]
  1 => array:1 [▼
    "content" => "myContent"
  ]
]

And there is the result I woulk like to have:

array:[
1234 => array(
        "title" => "myTitle"
        "type" => "myType"
        "content" => "myContent")
]

I don't understand why the code given on PHP.net work and mine doesn't

Code:

$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);

Result:

Array
(
    [color] => Array
        (
            [favorite] => Array
                (
                    [0] => red
                    [1] => green
                )

            [0] => blue
        )

    [0] => 5
    [1] => 10
)
share|improve this question
    
if the keys are numeric it will append the value instead of merging it into one array –  Jurgo 2 hours ago
    
I will use "id_124" as key, thanks. –  iBadGamer 2 hours ago

2 Answers 2

up vote 0 down vote accepted

Change the key 1234 to string like

 $Podcasts1 = array('a' => array('title' => "myTitle", "type" => "myType"));
 $Podcast2 = array('a' => array("content" => "myContent"));
 $podcasts = array_merge_recursive($Podcasts1, $Podcast2);
 var_dump($Podcasts1, $Podcast2, $podcasts);

array_merge_recursive works on string keys.

share|improve this answer
    
Thanks ! It's working and it's explain why my code doesn't work but I need theses ids to identify when it's the same podcast in both arrays.. Is there no solution except adding a string to the begining of the id (like "a124" wich is bad way to do what I want to do...) –  iBadGamer 2 hours ago
    
if you are using array_merge_recursive then you have to use string keys like id_1234. –  Ammar Hayder Khan 2 hours ago
    
Well it will do the trick I guess, thanks for your help –  iBadGamer 2 hours ago

It wont work for numeric key as function description says

If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too. If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.

Ref Link

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.