简体   繁体   English

拾取json数组中的条目数

[英]Picking up how many entries in a json array

I use the following code to extract information from a json page. 我使用以下代码从json页面提取信息。

$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. 目前仅存在2周。 1 new entry will be added every week. 每周将添加1个新条目。 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. 我想要做的就是将这些信息放入HTML表格中,我希望代码能够知道有多少周。 There will be 1 row of data for each week. 每周将有1行数据。

Let me know if this isn't clear.. and thanks! 如果不清楚,请告诉我..谢谢!

Use .length 使用.length

In Javascript 在Javascript中

jsonObj['summary'].length

In PHP 在PHP中

echo count($jsonarray['fixture_history']['summary']);

What you need is count() , which gives you the length of the array. 你需要的是count() ,它给你数组的长度。 Use that in a for-loop for a condition, and you should have your answer. 在条件的for循环中使用它,你应该得到你的答案。

$arr_length = count($arr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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