Reputation: 435
I have the following 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":[
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""
]
}
I'm loading that file and going trough it with foreach. The php code I have is as followed:
<?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 />";}
}
}
?>
I'm running PHP 5.5 on the webserver, I expect the code to result the following:
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
However for some reason it's only return the first character. I'm not sure what I did wrong. I'm getting the following:
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
Upvotes: 1
Views: 38
Reputation: 10646
I updated your code a bit so it works. Your issue is with treating the array as list($key, $val). Simply do:
$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 />";
}
}
}
}
Upvotes: 3