Skip to content

Default JSON Machine::FromFile return is Object. Is there a way for it to be array? #43

Answered by halaxa
dennisong45 asked this question in Q&A
Discussion options

You must be logged in to vote

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.
}

$section in this example is always an array. But be aware of #37

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by halaxa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants