如何将 json 格式转换为 php 数组


How to convert json format to php array

>我有一个data.json文件,其中包含这样的数据

    "meta": [
        "rectime",
        "strid",
        "ambt",
        "stri",
        "b1",
        "b2",
        "b3",
        "b4"
    ],
    "data": [
        [
            1377597739,
            1,
            0,
            77,
            816,
            13791,
            13794,
            13945
        ],
        [
            1377597739,
            2,
            0,
            0,
            816,
            13744,
            13725,
            13898
        ]
    ]
}

我想像这样将这些数据转换为 PHP 数组

<?php
header("Content-type: text/json");
$data = array(
  'John' => array(10,4,6,5), 
  'Jane' => array(3,4,2,3), 
  'Joe' => array(6,7,9,7)
);
echo json_encode($data);
?>

愿有人帮我提出建议。

感谢您的帮助

使用 json_decode()

$array = json_decode($json, true);

查看实际效果

您可以使用单行:

$data = json_decode(file_get_contents("data.json"), true);

但是,从文件粘贴的代码段不是有效的 JSON。 确保输入实际上是有效的 JSON,否则json_decode()将返回 NULL