如何在PHP中选择JSON(动态)父母和孩子



我有一个看起来像这样的JSON:

{
    "Meta Data": {
        "1. Information": "Monthly Prices (open, high, low, close) and Volumes",
        "2. Symbol": "AAPL",
        "3. Last Refreshed": "2017-05-31",
        "4. Time Zone": "US/Eastern"
    },
    "Monthly Time Series": {
        "2017-05-31": {
            "1. open": "145.1000",
            "2. high": "156.6500",
            "3. low": "144.2700",
            "4. close": "152.7600",
            "5. volume": "652734610"
        },
        "2017-04-28": {
            "1. open": "143.7100",
            "2. high": "145.4600",
            "3. low": "140.0600",
            "4. close": "143.6500",
            "5. volume": "373272701"
        },
        "2017-03-31": {
            "1. open": "137.8900",
            "2. high": "144.5000",
            "3. low": "137.0500",
            "4. close": "143.6600",
            "5. volume": "562091214"
        }
    }
}

我设法使用此代码选择了"每月时间序列"儿童:

$url = "http:...";   //some URL
$json = file_get_contents($url);   //get URL content
$data = json_decode($json,true);   //decode JSON
$arr = $data['Monthly Time Series'];   //Ignore the part before 'Monthly Time Series'
$someArray = $arr;   // Loop through the JSON array
  foreach ($someArray as $key => $value) {
    echo "[" .$value["1. open"] . ", " . $value["2. high"] . ", " . $value["3. low"] . ", " . $value["4. close"] . ", " . $value["5. volume"] . "],n";
  }
echo $somearray;

我的输出像这样:

[145.1000, 156.6500, 144.2700, 152.7600, 652734610],
[143.7100, 145.4600, 140.0600, 143.6500, 373272701],
[137.8900, 144.5000, 137.0500, 143.6600, 562091214]

我不太了解如何也可以选择日期。实际上,我需要与孩子一起包含的父母(日期((价值(。并且需要将日期转换为时间戳。因此结果看起来像这样:

[1496181600, 145.1000, 156.6500, 144.2700, 152.7600, 652734610],
[1493330400, 143.7100, 145.4600, 140.0600, 143.6500, 373272701],
[1490911200, 137.8900, 144.5000, 137.0500, 143.6600, 562091214]

我相信时间的格式是通过PHP的

完成的
strtotime

高度赞赏您身边的任何输入。请记住,我是绝对的PHP(和JS(Newbee ..

这有两种简洁的方法(一种评论(使用foreach()循环来达到所需的结果。(演示(:

$data=json_decode($json,true);
$arr=$data['Monthly Time Series'];
foreach($arr as $k=>$v){
    $result[]='['.strtotime($k).', '.implode(', ',$v).']';  // avoid naming each element
    // or $result[]='['.implode(', ',[strtotime($k)]+$v).']'; // merge then implode()
}
echo implode(",n",$result);  // no trailing comma on the final row

输出:

[1496214000, 145.1000, 156.6500, 144.2700, 152.7600, 652734610],
[1493362800, 143.7100, 145.4600, 140.0600, 143.6500, 373272701],
[1490943600, 137.8900, 144.5000, 137.0500, 143.6600, 562091214]

现在,如果您要将此数据传递给JavaScript并想要创建一个JSON数组,则可以使用以下一种方法之一使用以下定义的$arr

foreach($arr as $k=>$a){
    $result[]=[strtotime($k)]+array_values($a);  // merge the reformatted key with the values
}
echo json_encode($result);

或基于函数的方法没有结果数组变量声明:

$json=json_encode(
        array_map(
            function($key,$values){
                return [strtotime($key)]+array_values($values);  // merge
            },
            array_keys($arr),  // the keys
            array_column($arr,null)  // the values
        )
    );
echo $json;
// This method doesn't need to declare $json, it can be directly var_exported.
// I mere declared $json for consistency / comprehension.

这两种JSON-INTUPTITATINT方法都将产生:

[[1496214000,"156.6500","144.2700","152.7600","652734610"],[1493362800,"145.4600","140.0600","143.6500","373272701"],[1490943600,"144.5000","137.0500","143.6600","562091214"]]

这是一个新的演示。注意额外的[]包装结果;这使数据成为一个内部有多个数组的数组。
另请注意,strtotime日期之后的值被双重引用为字符串。这意味着将来的数据处理不会意外删除任何尾随的零 - 假设它们要保留很重要。

尝试一下希望这会有所帮助。将其添加到您的代码strtotime($key)

我不知道你要做什么。相反,我建议使用 json_encode

尝试此代码段

$data = json_decode($json, true);
$arr = $data['Monthly Time Series'];
$result=array();
foreach ($arr as $key => $value)
{
    $result[]=  sprintf("[%s, %s, %s, %s, %s, %s]",
                    strtotime($key),
                    $value["1. open"],
                    $value["2. high"],
                    $value["3. low"],
                    $value["4. close"],
                    $value["5. volume"]);
}
print_r(join(",n",$result));

最新更新