So I have this JSON string:
"{"Jan": [5, 10, 15, 20] ,
"Feb":[20,10,"",22],
"Mar":[5,3,"",4],
"April":[10,"",1,2]
},
{"title":"Test Chart - Month v Value"}"
I am trying to extract the data so that I can access each one so that I can iterate through the array and sequentially output the data.
The best way I can think of is to simply create an array of arrays in PHP, store key and its values together and do a nested for loop. So the array should look like this:
[[Jan, 5, 10, 15, 20],[Feb, 20, 10, ,22], [Mar, 5, 3, , 4]]
Lastly I would like to know how to store this title value into its own variable.
Your string isn't valid json currently, but it does contain two valid json strings. The json_decode() function won't work on your string as-is, but it does work on each individual valid json string:
<?php
// INVALID JSON STRING - THIS WON'T WORK:
$json = '{"Jan": [5, 10, 15, 20] , "Feb":[20,10,"",22], "Mar":[5,3,"",4], "April":[10,"",1,2]}, {"title":"Test Chart - Month v Value"}';
print_r(json_decode($json));
// This works:
$json = '{"Jan": [5, 10, 15, 20] , "Feb":[20,10,"",22], "Mar":[5,3,"",4], "April":[10,"",1,2]}';
print_r(json_decode($json));
// This works:
$json = '{"title":"Test Chart - Month v Value"}';
print_r(json_decode($json));
?>
Ideally you would fix the source json, but it might be getting passed to you or retrieved from a system you have no control over.
If you can safely assume that the json will always have a structure like in your example, you could split the strings out doing something like this:
if (preg_match_all("/{[^}]*}/", $str, $matches)) {
foreach ($matches[0] AS $json) {
print_r(json_decode($json));
}
}
This won't work if the json object contains other objects, which is why it depends on whether you can safely assume the structure will always be the same or not.
Try this code
<?php
$jsonData = '[{
"Jan": [5, 10, 15, 20],
"Feb": [20, 10, "", 22],
"Mar": [5, 3, "", 4],
"April": [10, "", 1, 2]
}, {
"title": "Test Chart - Month v Value"
}]';
$array = json_decode($jsonData ,true);
$formattedArr = array();
$i = 0;
foreach($array[0] as $key=>$val) {
$formattedArr[$i] = $val;
array_unshift($formattedArr[$i],$key);
$i++;
}
echo "<pre>";
print_r($formattedArr);
?>
You should format the JSON to what Barmar suggested (check your comments), currently your JSON string is not formatted properly.
$json = '{"title":"Test Chart - Month v Value", "data": {"Jan": [5, 10, 15, 20], "Feb":[20,10,"",22], "Mar":[5,3,"",4], "April":[10,"",1,2]}}'
This would change the string to the following PHP array:
print_r($arr = json_decode($json, true));
Array
(
[title] => Test Chart - Month v Value
[data] => Array
(
[Jan] => Array
(
[0] => 5
[1] => 10
[2] => 15
[3] => 20
)
[Feb] => Array
(
[0] => 20
[1] => 10
[2] =>
[3] => 22
)
[Mar] => Array
(
[0] => 5
[1] => 3
[2] =>
[3] => 4
)
[April] => Array
(
[0] => 10
[1] =>
[2] => 1
[3] => 2
)
)
)
Now you can access the data like so:
echo $arr['title'];
print_r($arr['data']);
print_r($arr['data']['Jan']);
$data = '{"Jan": [5, 10, 15, 20] , "Feb":[20,10,"",22], "Mar":[5,3,"",4], "April":[10,"",1,2],"title":"Test Chart - Month v Value"}';
$out = json_decode($data, true);
$newArray = array();
foreach ($out as $key=>$value){
if(is_array($value)){
foreach($value as $key1=>$row){
$value[$key1+1] = $row;
}
$value[0] = $key;
$newArray[] = $value;
}
}
echo json_encode($newArray);