0

I have a big json file containing an array of objects. The file is too big (15MB) to be parsed by json_decode. How could I split this array-file into multiple array-files?

The array contains objects, which may also contain objects within it.

Maciej Krawczyk
  • 14,825
  • 5
  • 55
  • 67

2 Answers2

1

you can try array chunking -

$halved_array = array_chunk($original_array, ceil(count($original_array)/2));
Sujit Agarwal
  • 12,348
  • 11
  • 48
  • 79
0

You can't chuck the file without having the whole string in memory at any time. You may need to work on the source of the problem (if you can) instead of trying to deal with it. However you can setup a cron than will have for only job to chuck this JSON file into multiple JSON files.

Geolim4
  • 454
  • 3
  • 14
  • 1
    I guess there should be a way. If we could parse the file backwards, character by character. Find matching [ and cut the parts after some amount of data has been parsed and save it to another file. – Maciej Krawczyk Jul 30 '18 at 14:48
  • It would not be reliable at all, i highly discourage you to parse a json file file outside the JSON extension scope. – Geolim4 Jul 30 '18 at 15:39