每个列表只返回1个字母

时间:2015-09-23 17:43:31

标签: php list foreach

我有以下JSON:

   {
   "Maandag":[  
      "---. --- --- --- wo 6+7.",
      "---. --- --- --- wo 6+7.",
      "CTERCK33. reactorkunde lina L11.02 ma 3+4.",
      "CTERCK33. reactorkunde lina L10.01 ma 3+4.",
      "--- --- --- --- ma 6+7",
      "--- --- --- --- ma 6+7",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      ""
   ],
   "Dinsdag":[  
      "CTEENG31 engels oosj L11.03 di 5+6",
      "CTEENG31 engels oosj L11.03 di 5+6",
      "",
      "",
      "CTEPSUTU project suiker smij L11.02 di 1+2",
      "CTEPSUTU project suiker smij L11.02 di 1+2",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      ""
   ],
   "Woensdag":[  
      "CTEMCH33 mechanica akba L9.02 wo 1+2",
      "CTEMCH33 mechanica akba L9.02 wo 1+2",
      "CTEMCH33 mechanica akba L9.02 wo 1+2",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      ""
   ],
   "Donderdag":[  
      "",
      "",
      "CTEWIS51 wiskunde abdos L10.01",
      "CTEWIS51 wiskunde abdo L10.01 do 4+5",
      "CTEWIS51 wiskunde abdo L11.02 do 4+5",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      ""
   ],
   "Vrijdag":[  
      "",
      "",
      "",
      "",
      "",
      "CTERCK33. reactorkunde lina L7.09 ma 1+2.",
      "CTERCK33. reactorkunde lina L7.09 ma 1+2.",
      "CTEPSUTU project suiker oosj L10.13 ma 8+9",
      "CTEPSUTU project suiker oosj L10.13 ma 8+9",
      "",
      "",
      "",
      "",
      "",
      ""
   ],
   "Zaterdag":[  
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      "",
      ""
   ]
}

我正在加载该文件并将其与foreach一起使用。我的PHP代码如下:

<?php
$json = file_get_contents('jsonoutput.json');

//convert json object to php associative array
    $data = json_decode($json, true);

//json
$i = 0;
foreach ($data as $singleArray) {
    $i++;
    $o = 0;                         
    foreach ($singleArray as list($key, $value)) {
        $o++;

        //Remove Empty Hours
        if (!empty($value)){
        echo "$i - $key: $value<br />";}
        }
    }
?>

我在网络服务器上运行PHP 5.5,我希望代码能产生以下结果:

1 - 0: ---. --- --- --- wo 6+7.
1 - 1: ---. --- --- --- wo 6+7.
1 - 2: CTERCK33. reactorkunde lina L11.02 ma 3+4.
1 - 3: CTERCK33. reactorkunde lina L10.01 ma 3+4.
1 - 4: --- --- --- --- ma 6+7
1 - 5: --- --- --- --- ma 6+7
2 - 0: CTEENG31 engels oosj L11.03 di 5+6
2 - 1: CTEENG31 engels oosj L11.03 di 5+6
2 - 4: CTEPSUTU project suiker smij L11.02 di 1+2
2 - 5: CTEPSUTU project suiker smij L11.02 di 1+2
3 - 0: CTEMCH33 mechanica akba L9.02 wo 1+2
3 - 1: CTEMCH33 mechanica akba L9.02 wo 1+2
3 - 2: CTEMCH33 mechanica akba L9.02 wo 1+2
4 - 2: CTEWIS51 wiskunde abdos L10.01
4 - 3: CTEWIS51 wiskunde abdo L10.01 do 4+5
4 - 4: CTEWIS51 wiskunde abdo L11.02 do 4+5
5 - 5: CTERCK33. reactorkunde lina L7.09 ma 1+2.
5 - 6: CTERCK33. reactorkunde lina L7.09 ma 1+2.
6 - 7: CTEPSUTU project suiker oosj L10.13 ma 8+9
6 - 8: CTEPSUTU project suiker oosj L10.13 ma 8+9

但由于某种原因,它只返回第一个字符。我不确定我做错了什么。我得到以下内容:

1 - -: -
1 - -: -
1 - C: T
1 - C: T
1 - -: -
1 - -: -
2 - C: T
2 - C: T
2 - C: T
2 - C: T
3 - C: T
3 - C: T
3 - C: T
4 - C: T
4 - C: T
4 - C: T
5 - C: T
5 - C: T
5 - C: T
5 - C: T

1 个答案:

答案 0 :(得分:3)

我更新了你的代码,所以它的工作原理。您的问题是将数组视为列表($ key,$ val)。只需:

$data = json_decode($json);
$i = 0;
foreach ($data as $singleArray) {
    $i++;
    $o = 0; 
    if(is_array($singleArray)){                     
        foreach($singleArray as $key => $val) {             
                $o++;   
                //Remove Empty Hours
                if (!empty($val)){
                    echo "$i - $key: $val<br />";
                }

        }
    }
}