I need to export a huge php multidimensional array containing the following structure:
[0]
['id'] => 0
['items']
[0] => "ItemA"
[1] => "ItemB"
['vars']
[0]
[0] => "blue"
[1] => "green"
[2] => "red"
[1]
[0] => "happy"
[1] => "sad"
[2]
[0] => "old"
[1]
['id'] => 1
['items']
[0] => "ItemX"
['vars']
[0]
[0] => "orange"
[1] => "pink"
[1]
[0] => "bitter"
[2]
[0] => "new"
[1] => "modern"
…and so on.
As you see, the sizes of the sub-arrays are not always the same ( [0][‘items’] = 2 vs. [1][‘items’] = 1).
I need to write an .CSV-File which later will be read by a JavaScript-function, which should reassemble the original structure.
What should a record in the .CSV-File look like to enable this and make it as easy as possible?
Thank you!
Simply use JSON which perfectly fits such requirements. Representing nested data with CSV would be madness (as already pointed out in some of the comments).
In PHP you can use
json_encode()to encode the data as JSON and to decode it, usejson_decode(). As the name suggests it (JavaScript Object Notation) JavaScript is perfectly fit to handle JSON formatted data.