Reputation: 607
I have the following JSON format:
{
"S01": ["001.cbf", "002.cbf", "003.cbf", "004.cbf", "005.cbf", "006.cbf", "007.cbf", "008.cbf", "009.cbf"],
"S02": ["001.sda", "002.sda", "003.sda"],
"S03": ["001.klm", "002.klm"]
}
I try using this code:
$json = json_decode('{"S01":["001.cbf","002.cbf","003.cbf","004.cbf","005.cbf","006.cbf","007.cbf","008.cbf","009.cbf"],"S02":["001.sda","002.sda","003.sda"],"S03":["001.klm","002.klm"]}');
foreach($json as $key => $val) {
if ($key) { echo 'KEY IS: '.$key; };
if ($val) { echo 'VALUE IS: '.$value; };
echo '<br>';
}
But i got empty results...i need to get output like this:
KEY IS: S01
VALUE IS: 001.cbf
VALUE IS: 002.cbf
VALUE IS: 003.cbf
VALUE IS: 004.cbf
VALUE IS: 005.cbf
VALUE IS: 006.cbf
VALUE IS: 007.cbf
VALUE IS: 008.cbf
VALUE IS: 009.cbf
KEY IS: S02
VALUE IS: 001.sda
VALUE IS: 002.sda
VALUE IS: 003.sda
KEY IS: S03
VALUE IS: 001.klm
VALUE IS: 002.klm
This i need so that i can generate ul and li elements using value and key name...this is JSON format that is stored in mysql database and is read to php script that needs to parse the JSON in the above output so that i can create ul and li elements using output.
I try to do foreach but i got empty results? I know when i got value that i need to do explode string using explode(', ', $value)
but i can't get $value and $key to be read as needed in PHP.
Upvotes: 6
Views: 48358
Reputation: 5507
If you performed a var_dump($json) to observe the result you would see it looks like this...
$json = json_decode('{"S01":["001.cbf","002.cbf","003.cbf","004.cbf","005.cbf","006.cbf","007.cbf","008.cbf","009.cbf"],"S02":["001.sda","002.sda","003.sda"],"S03":["001.klm","002.klm"]}');
var_dump($json);
object(stdClass)[1]
public 'S01' =>
array (size=9)
0 => string '001.cbf' (length=7)
1 => string '002.cbf' (length=7)
2 => string '003.cbf' (length=7)
3 => string '004.cbf' (length=7)
4 => string '005.cbf' (length=7)
5 => string '006.cbf' (length=7)
6 => string '007.cbf' (length=7)
7 => string '008.cbf' (length=7)
8 => string '009.cbf' (length=7)
public 'S02' =>
array (size=3)
0 => string '001.sda' (length=7)
1 => string '002.sda' (length=7)
2 => string '003.sda' (length=7)
public 'S03' =>
array (size=2)
0 => string '001.klm' (length=7)
1 => string '002.klm' (length=7)
So you effectively have an array of arrays.
So for each "key" the associated "val" is an array that you have to loop through.
So you need to iterate through each key, then iterate through each val array.
foreach ($json as $key => $val) {
echo 'KEY IS: ' . $key;
echo '<br>';
foreach ($val as $value) {
echo 'VALUE IS: ' . $value;
echo '<br>';
}
echo '<br>';
}
The resulting output is...
KEY IS: S01
VALUE IS: 001.cbf
VALUE IS: 002.cbf
VALUE IS: 003.cbf
VALUE IS: 004.cbf
VALUE IS: 005.cbf
VALUE IS: 006.cbf
VALUE IS: 007.cbf
VALUE IS: 008.cbf
VALUE IS: 009.cbf
KEY IS: S02
VALUE IS: 001.sda
VALUE IS: 002.sda
VALUE IS: 003.sda
KEY IS: S03
VALUE IS: 001.klm
VALUE IS: 002.klmlm
VALUE IS: 002.klm
Upvotes: 5
Reputation: 1037
This solves your problem, you had to cast $json to array because it was considered as an stdClass object ;)
<?php
$json = json_decode('{"S01":["001.cbf","002.cbf","003.cbf","004.cbf","005.cbf","006.cbf","007.cbf","008.cbf","009.cbf"],"S02":["001.sda","002.sda","003.sda"],"S03":["001.klm","002.klm"]}');
foreach($json as $key => $val) {
echo "KEY IS: $key<br/>";
foreach(((array)$json)[$key] as $val2) {
echo "VALUE IS: $val2<br/>";
}
}
?>
I recommend you to use the function var_dump($var) next time you run into troubles, it will help you to figure out what's wrong.
Upvotes: 9
Reputation: 1023
$json = '{"S01":["001.cbf","002.cbf","003.cbf","004.cbf","005.cbf","006.cbf","007.cbf","008.cbf","009.cbf"],"S02":["001.sda","002.sda","003.sda"],"S03":["001.klm","002.klm"]}';
$array = json_decode($json, true);
foreach($array as $key => $val) {
echo 'KEY IS:'.$key.'<br/>';
foreach($val as $_key => $_val) {
echo 'VALUE IS: '.$_val.'<br/>';
}
}
Upvotes: 6