Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 881
Cully Avatar asked Oct 04 '22 17:10

Cully


2 Answers

Use .length

In Javascript

jsonObj['summary'].length

In PHP

echo count($jsonarray['fixture_history']['summary']);
like image 124
Dipesh Parmar Avatar answered Oct 10 '22 07:10

Dipesh Parmar


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);
like image 37
Daedalus Avatar answered Oct 10 '22 07:10

Daedalus