Default JSON Machine::FromFile return is Object. Is there a way for it to be array? #43
-
Hi first and foremost, thank you for this excellent repo. I have a script that read and write alot of JSON as fast as possible in a infinite while loop. Your library makes things so much faster. But i can't fully integrate yet because usually. I usually Example of JSON
This is possible due it is an array. Typecast convert to array after using JSONMACHINE, the error array will be corrupted as well Would like to return as an array instead of going another for loop inside. is it possible? Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi. The big advantage of JsonMachine is that you can lazily iterate any subtree in the document. See https://github.com/halaxa/json-machine#parsing-a-subtree. In your case try this: $sections = JsonMachine::fromFile('file.json', '/sections') // it means you only want to iterate over items in `sections` subtree one by one.
foreach ($sections as $key => $section) {
echo $key // will get 2d4
echo $section["n"] // will get MY SECTION
echo $section["ls"][0]["n"] // will get n = v1.
}
|
Beta Was this translation helpful? Give feedback.
Hi.
The big advantage of JsonMachine is that you can lazily iterate any subtree in the document. See https://github.com/halaxa/json-machine#parsing-a-subtree.
In your case try this:
$section
in this example is always an array. But be aware of #37