如何解码PHP中的JSON嵌套数组值?

时间:2016-11-17 04:15:33

标签: php arrays json

所以我有这个JSON字符串:

"{"Jan": [5, 10, 15, 20] , 
  "Feb":[20,10,"",22], 
  "Mar":[5,3,"",4], 
  "April":[10,"",1,2]
 }, 
 {"title":"Test Chart - Month v Value"}"

我正在尝试提取数据,以便我可以访问每个数据,以便我可以遍历数组并按顺序输出数据。

我能想到的最好方法是简单地在PHP中创建一个数组数组,将键及其值存储在一起并执行嵌套for循环。所以数组应该如下所示:

[[Jan, 5, 10, 15, 20],[Feb, 20, 10, ,22], [Mar, 5, 3, , 4]]

最后,我想知道如何将此标题值存储到自己的变量中。

4 个答案:

答案 0 :(得分:0)

您的字符串当前不是有效的json,但它确实包含两个有效的json字符串。 json_decode()函数不会按原样处理你的字符串,但它确实对每个有效的json字符串起作用:

<?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));

?>

理想情况下,您可以修复源json,但它可能会传递给您或从您无法控制的系统中检索。

如果您可以放心地假设json将始终具有类似于您的示例中的结构,则可以将字符串拆分为:

if (preg_match_all("/{[^}]*}/", $str, $matches)) {
  foreach ($matches[0] AS $json) {
    print_r(json_decode($json));
  }
}

如果json对象包含其他对象,这将不起作用,这就是为什么它取决于你是否可以安全地假设结构总是相同或不同。

答案 1 :(得分:0)

试试此代码

<?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);

?>

答案 2 :(得分:0)

您应该将JSON格式化为Barmar建议的内容(检查您的注释),目前您的JSON字符串格式不正确。

$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]}}'

这会将字符串更改为以下PHP数组:

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
                )

        )

)

现在您可以像这样访问数据:

echo $arr['title'];
print_r($arr['data']);
print_r($arr['data']['Jan']);

答案 3 :(得分:0)

$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);