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 am trying to loop through some json files, and combine them into one json file. My plan is to have a global $allData array, and just merge new candidates into them.

<?php
$allData = array();
$count = 0;
if ($handle = opendir('./json/')) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {

            echo $entry."<br />";

            $source_file = file_get_contents('./json/'.$entry);

            $data = json_decode($source_file,TRUE);

            if($data != null){


                $allData = array_merge($allData,$data);
                echo "<br /><br /><br /><br /> !!!! <br /><br /><br /><br />";
                print_r($allData);

            }
            else{
                echo "Invalid Json File";
            } //end else            
        }
closedir($handle);
}

echo "<br /><br /><br /><br /> !!!! <br /><br /><br /><br />";

print_r($allData);  

However, the merge is overwriting the file. How can I combine multiple json files into one?

I would like the following result:

1.json:

{"date":"10"},{"comment":"some comment"},{"user":"john"}

2.json:

{"date":"11"},{"comment":"another quote"},{"comment":"jim"}

combined.json

[{"date":"10"},{"comment":"some comment"},{"user":"john"},
{"date":"11"},{"comment":"another quote"},{"comment":"jim"}]

I am only getting one of these values after I merge the arrays.

[{"date":"25.4.2013 10:40:10"},{"comment":"some text"},{"comment":"some more text"},
[{"date":"25.4.2013 10:45:15"},{"comment":"another quote"},{"comment":"quote"}]]
share|improve this question
    
Have you tryed using array_merge() with json_decode()'ed data? –  PKeidel Dec 8 '13 at 22:37

2 Answers 2

up vote 1 down vote accepted

Your merge is odd:

$result = array_merge($allData,$data);

You want to be merging each new $data array onto a growing $allData array right? I think you want to do this instead:

$allData = array_merge($allData,$data);

Also you can get rid of this, it's not necessary.

if($count == 0){
    $allData = $data;
}
share|improve this answer
    
I have logic so I don't overwrite it. And $allData is already declared above. –  publ1c_stat1c Dec 8 '13 at 22:38
    
@publ1c_stat1c See my updated answer. I think $result is complicating things, and you don't need it. Merge $data with $allData and set it as the new $allData each time in your loop. –  jszobody Dec 8 '13 at 22:39
    
I have pursued this, and I still do not get the desired result. It is still getting overwritten. –  publ1c_stat1c Dec 8 '13 at 22:41
    
@publ1c_stat1c It won't get overwritten if you do it just like I suggested. Feel free to update your question and show your code attempt with this merge if you have issues with it, and I can help show you what's wrong. –  jszobody Dec 8 '13 at 22:43
    
@publ1c_stat1c Here is a really simple example of how your two JSON strings will merge quite nicely: 3v4l.org/p1bVL. Give me a simple code example of how it's failing for you, and I'll be happy to help. –  jszobody Dec 8 '13 at 22:45

As stated before, $result = array_merge($allData,$data); is the best way to merge arrays. This code works to merge the two, however, the problem was that my json was not properly formed.

I didn't have [] around my two files, so they could not be merged properly.

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.