按键分组/组合json数组

时间:2016-03-11 21:09:54

标签: php json csv

我怎么能把它变成这样(完整的json就像下面一样,但每5分钟就会出现一个新的时间戳)。我一次拉一整天的数据

[
{
  "Timestamp":"2016-03-11T14:40:00+00:00",
  "Point":"ategho-leg_1-8",
  "Value":"487.0"
},
{
  "Timestamp":"2016-03-11T14:40:00+00:00",
  "Point":"ategho-leg_2-8",
  "Value":"488.0"
},
{
  "Timestamp":"2016-03-11T14:40:00+00:00",
  "Point":"ategho-leg_3-8",
  "Value":"484.0"
},
{
  "Timestamp":"2016-03-11T14:40:00+00:00",
  "Point":"ategho-temperature_long-8",
  "Value":"466.0"
},
{
  "Timestamp":"2016-03-11T14:40:00+00:00",
  "Point":"ategho-temperature_short-8",
  "Value":"198.0"
},
{
  "Timestamp":"2016-03-11T14:45:00+00:00",
  "Point":"ategho-leg_1-8",
  "Value":"487.0"
},
{
  "Timestamp":"2016-03-11T14:45:00+00:00",
  "Point":"ategho-leg_2-8",
  "Value":"488.0"
},
{
  "Timestamp":"2016-03-11T14:45:00+00:00",
  "Point":"ategho-leg_3-8",
  "Value":"484.0"
},
{
  "Timestamp":"2016-03-11T14:45:00+00:00",
  "Point":"ategho-temperature_long-8",
  "Value":"466.0"
},
{
  "Timestamp":"2016-03-11T14:45:00+00:00",
  "Point":"ategho-temperature_short-8",
  "Value":"198.0"
},
{
  "Timestamp":null,
  "Point":null,
  "Value":null
}
]

进入这个:

[
{
  "Timestamp":"2016-03-11T14:40:00+00:00",
  "ategho-leg_1-8" : "487.0",
  "ategho-leg_2-8" : "488.0",
  "ategho-leg_3-8" : "484.0",
  "ategho-temperature_long-8" : "466.0",
  "ategho-temperature_short-8" : "198.0"
},
{
  "Timestamp":"2016-03-11T14:45:00+00:00",
  "ategho-leg_1-8" : "487.0",
  "ategho-leg_2-8" : "488.0",
  "ategho-leg_3-8" : "484.0",
  "ategho-temperature_long-8" : "466.0",
  "ategho-temperature_short-8" : "198.0"
}
]

这是我尝试过但不起作用

 $datas = json_decode($morris, true);
$out = [];

foreach($datas as $element) {
    $out[$element['Timestamp']][] = ['Date' => $element['Timestamp'], 
  'Point' => $element['Point'], 'Value' => $element['Value']];
 }

$thing = json_encode($out);

输出:

{
"2016-03-11T14:40:00+00:00":[
  {
     "Timestamp":"2016-03-11T14:40:00+00:00",
     "Point":"ategho-leg_1-8",
     "Value":"487.0"
  },
  {
     "Timestamp":"2016-03-11T14:40:00+00:00",
     "Point":"ategho-leg_2-8",
     "Value":"488.0"
  },
  {
     "Timestamp":"2016-03-11T14:40:00+00:00",
     "Point":"ategho-leg_3-8",
     "Value":"484.0"
  },
  {
     "Timestamp":"2016-03-11T14:40:00+00:00",
     "Point":"ategho-temperature_long-8",
     "Value":"466.0"
  },
  {
     "Timestamp":"2016-03-11T14:40:00+00:00",
     "Point":"ategho-temperature_short-8",
     "Value":"198.0"
  }
 ],

1 个答案:

答案 0 :(得分:1)

应该是这样的:

foreach ($datas as $element) {
    $out["Timestamp"] = $element["Timestamp"];
    $out[$element["Point"]] = $element["Value"];
}

我带了你的数据:

$r = '[
{
  "Timestamp":"2016-03-11T14:40:00+00:00",
  "Point":"ategho-leg_1-8",
  "Value":"487.0"
},
{
  "Timestamp":"2016-03-11T14:40:00+00:00",
  "Point":"ategho-leg_2-8",
  "Value":"488.0"
},
{
  "Timestamp":"2016-03-11T14:40:00+00:00",
  "Point":"ategho-leg_3-8",
  "Value":"484.0"
},
{
  "Timestamp":"2016-03-11T14:40:00+00:00",
  "Point":"ategho-temperature_long-8",
  "Value":"466.0"
},
{
  "Timestamp":"2016-03-11T14:40:00+00:00",
  "Point":"ategho-temperature_short-8",
  "Value":"198.0"
}
]';
$datas = json_decode($r, true);
$out = [];

foreach ($datas as $element) {
    $out["Timestamp"] = $element["Timestamp"];
    $out[$element["Point"]] = $element["Value"];
}

echo '<pre>',print_r($out),'</pre>';

输出是:

Array
(
    [Timestamp] => 2016-03-11T14:40:00+00:00
    [ategho-leg_1-8] => 487.0
    [ategho-leg_2-8] => 488.0
    [ategho-leg_3-8] => 484.0
    [ategho-temperature_long-8] => 466.0
    [ategho-temperature_short-8] => 198.0
)

这意味着您没有提供足够的信息。