Reputation: 159
I have a file called data.txt in the public path , it has this
{"data":[{"name": "Jack", "age": 13},{"name": "Mary", "age": 15}]}
and I want to read these data and also return the age for both Jack and Mary,
I know that I can read the file using the decode_json(file_get_contents) but I have no clue how to fetch the data and return specific records from it.
Any help is much appreciated !
Upvotes: 0
Views: 4496
Reputation: 40909
You can load the file and decode the content with:
$content = json_decode(file_get_contents($path), true);
This will give you the associative array that contains your JSON content.
You can access data in user objects like that:
$userData = $content['data']; // content of "data" field
$jackData = $userData[0]; // first object in "data" array - Jack
$jackName = $jackData['name']; // Jack's name
Upvotes: 3