Q:

Merge Json objects, regardless of Memory

OK, here's what I need :

I've got several (lots of them actually) json objects written to files (e.g. result.1.json, result.2.json, etc)
I need to combine all of them in an array, like :
$results = array (
    json_decode(file_get_contents("result.1.json"),      
    json_decode(file_get_contents("result.2.json")), 
    ...
)

And then write all $results back to a json file, like :
file_put_contents("results.json",$results);

And here's the issue :

if we are talking about LOTS of json objects, am I going to face a Memory limit error (that's why I decided to split the objects in the first place - storing all results in memory triggered a Memory related error and crashed)?
If the above is true, how could I circumvent it, and still "concat" the objects?

A:

If the objects from the file(s) get enclosed in to just one big array, there's actually no need to read and decode them, assumed they actually are valid JSON Syntax.
An Array is of the kind:
[
    { obj1},
    { obj2}
]

So you can just do the following:
file_put_contents("results.json","[\n");
while ( ... has more files ... )
{
   file_put_contents(file_get_contents($theFile), FILE_APPEND);
   if ( .... has more files ....)
         file_put_contents("results.json",",\n", FILE_APPEND);
}
file_put_contents("results.json","]\n", FILE_APPEND);

This will use almost no memory and is quite fast.

