I use the following code to extract information from a json page.
$str = file_get_contents('http://fantasy.mlssoccer.com/web/api/elements/498/');
$jsonarray = json_decode($str, true);
$week1 = $jsonarray['fixture_history']['summary'][0][2];
$week2 = $jsonarray['fixture_history']['summary'][1][2];
Here's an excerpt of what it's taking from
{ "summary" :
[
[ 1, "PHI (A)", 14 ]
[ 2, "TOR (A)", 8 ]
]
}
At the moment only 2 weeks exist. 1 new entry will be added every week. How do I code something to say "loop for however many weeks/entries exist"?
Pretty much what I want to do is put this info into a HTML table and I want the code to know how many weeks are in there. There will be 1 row of data for each week.
Let me know if this isn't clear.. and thanks!
Use .length
In Javascript
jsonObj['summary'].length
In PHP
echo count($jsonarray['fixture_history']['summary']);
What you need is count()
, which gives you the length of the array. Use that in a for-loop for a condition, and you should have your answer.
$arr_length = count($arr);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With