1

To get the details with regards to "ID", I have prepared the following PHP with json array inside it. It is working perfectly.

<?php $json = '{"records": [ {"Name":"Jhon", "Age":"45", "id":"101"}, {"Name":"Bhil", "Age":"42", "id":"102"}, {"Name":"Tone", "Age":"41", "id":"103"}, ] }'; $myjson = json_decode($json, true); foreach ( $myjson['records'] as $row ) { if ($row['id'] =='216') { echo 'Details of login id:&nbsp;'. $row['id']. '<br/>'; foreach ( $row as $field => $value ) { // loop the fields // do the work here } } } ?> 

Now for one of my project, i have to crete arround 750 Ids with various vaules. Putting all these inside php file may slow down the page loading.

Hence, I have craeted json file(remote): xyz.com/dir/jsondata.json

Kindly guide me, how to incopporate the above json file in the php code.

1

2 Answers 2

1

file_get_contents() is what you're looking for. It will put the entire contents of the file into a string.

$json = file_get_contents('dir/jsondata.json'); 
Sign up to request clarification or add additional context in comments.

5 Comments

In case the remote file is csv: like xyz.com/dir/data.csv
file_get_contents() will work with any file type. If using a CSV file, you'd have to parse through the string to create your JSON array.
Is so, how to create the json array from the csv file
That is a whole new question entirely. I suggest you ask a new question and post a link to it here.
0

fetch the file using $data=file_get_contents('xyz.com/dir/jsondata.json'); and then use the decode the json and loop through it

$myjson = json_decode($data, true); foreach ( $myjson['records'] as $row ) { if ($row['id'] =='216') { echo 'Details of login id:&nbsp;'. $row['id']. '<br/>'; foreach ( $row as $field => $value ) { // loop the fields // do the work here } } } 

2 Comments

My question is: whether the remote json file is fast loading comaparing to json array inside php?
It would be slightly faster to include the json array within the php file rather than using file_get_contents()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.