我今天对阵列太累了 - 把我扔到了这个地方。
所以,这是数组的输出:
Array
(
[2010091907] => Array
(
[home] => Array
(
[score] => Array
(
[1] => 7
[2] => 17
[3] => 10
[4] => 7
[5] => 0
[T] => 41
)
[abbr] => ATL
[to] => 2
)
Array
(
[2010091909] => Array
(
[home] => Array
(
[score] => Array
(
[1] => 7
[2] => 17
[3] => 10
[4] => 7
[5] => 0
[T] => 41
)
[abbr] => ATL
[to] => 2
)
Array
(
[2010091901] => Array
(
[home] => Array
(
[score] => Array
(
[1] => 7
[2] => 17
[3] => 10
[4] => 7
[5] => 0
[T] => 41
)
[abbr] => ATL
[to] => 2
)
我将编写一个preg_match来迭代每个[2010091907]
,但在我可以之前,我不明白如何获取这些信息,或者如何调用它。我会做的事情如下:
$json=json_decode($data,true);
foreach ($json['dont-know-what-to-call-it'] as $key => $value) {
echo "Key: ".$key."; Value: ".$value."<br />";
}
我只是不知道如何调用那些[2010091901]块中的每一个,就像我想要称之为的名称一样。我知道如何将这些东西称为得分,因为它被命名为“得分”,数据就在所有这些之下。我不知道如何获取数组的初始“部分”的键/值。最后,我想要抓住每个[2010091901],操纵/使用[2010091901]中每一个之间的数据,然后转到下一个“记录”。
答案 0 :(得分:2)
$date_keys = array_keys($json)
会给(0 => 2010091907, 1 => 2010091909, ...)
。那么你可以做到
foreach ($date_keys as $d) {
foreach ($json[$d] as $key => $value) {
...
另外,如果你实际上不需要外部数组的索引(日期值 - 2010091907等),那么
foreach ($json as $j) {
foreach ($j as $key => $value) {
...
忽略$json
答案 1 :(得分:1)
你不能只嵌套foreach()
吗?
foreach($jsondata as $somedate => $value){
//do you actually need $somedate?
foreach($value['home']['score'] as $score){
echo $score.PHP_EOL;
}
}
答案 2 :(得分:0)
你可以做到
$json = json_decode($data, true);
foreach($json as $ymd => $data)
{
// $ymd = [2010091907, 2010091909,… ]
// $data is the array starting with the key home. so $data['home']['score'][1] = 7 for the first iteration
}
这回答你的问题?你所要求的并不是100%清楚