简体   繁体   中英

Picking up how many entries in a json array

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM