我有以下内容:
$data = [
{model_num : "ABC", revision : "AA", "testRecipeID":85, value : 31.25, treatment : 'Pressure' },
{model_num : "ABC", revision : "AA", "testRecipeID":85, value : 31.25, treatment : 'Gas' },
{model_num : "ABC", revision : "AA", "testRecipeID":85, value : 33.12, treatment : 'Temp' },
{model_num : "ABC", revision : "AA", "testRecipeID":85, value : 25.87, treatment : 'Current' },
{model_num : "ABC", revision : "AB", "testRecipeID":86, value : 26.63, treatment : 'Pressure' },
{model_num : "ABC", revision : "AB", "testRecipeID":86, value : 26.00, treatment : 'Gas' },
{model_num : "ABC", revision : "AB", "testRecipeID":86, value : 23.75, treatment : 'Temp' }
];
我想最终得到这样的结果:
var data=[{model_num : "ABC", revision : "AA", "testRecipeID":85, "Pressure":31.25, "Gas":31.25, "Temp": 33.12,"Current":25.87 },{model_num : "ABC", revision : "AB", "testRecipeID":86, "Gas":26.00,"Temp":23.75}]
我知道如何在JS中执行此操作,但不知道如何在PHP中执行此操作,结果我需要在我的php中执行此操作,以便我可以处理我拥有的大量数据。我有这个基于我发现的另一个问题,但它没有工作它返回0
$table = array();
$round_names = array();
$total = array();
foreach ($data as $score)
{
$round_names[] = $score->treatment;
$table[$score->testRecipeID][$score->treatment] = $score->value;
$total[$score->testRecipeID] += $score->value;
print_r($round_names);
}
$round_names = array_unique($round_names);
foreach ($table as $player => $rounds)
{
echo "$player\t";
foreach ($round_names as $round)
echo "$rounds[$round]\t";
echo "$total[$player]\n";
}
任何帮助将不胜感激!
这就是我在JS中的表现
var result = [];
data.forEach(function(e) {
var a = e.model_num + '|' + e.revision+ '|'e.recipeID;
if(!this[a]) {
this[a] = {model_num: e.model_num, revision: e.revision, recipeID: e.recipeID}
result.push(this[a]);
}
this[a][e.treatment] = e.value;
}, {});
答案 0 :(得分:1)
如果您需要该结构,可以尝试使用JSON Encode:
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
哪个输出:
{"a":1,"b":2,"c":3,"d":4,"e":5}
如果您需要它作为数组,请使用:
echo json_encode(array($arr));
答案 1 :(得分:-1)
这是PHP中的JS函数
<?php
$data = '[
{"model_num" : "ABC", "revision" : "AA", "testRecipeID":85, "value" : 31.25, "treatment" : "Pressure" },
{"model_num" : "ABC", "revision" : "AA", "testRecipeID":85, "value" : 31.25, "treatment" : "Gas" },
{"model_num" : "ABC", "revision" : "AA", "testRecipeID":85, "value" : 33.12, "treatment" : "Temp" },
{"model_num" : "ABC", "revision" : "AA", "testRecipeID":85, "value" : 25.87, "treatment" : "Current" },
{"model_num" : "ABC", "revision" : "AB", "testRecipeID":86, "value" : 26.63, "treatment" : "Pressure" },
{"model_num" : "ABC", "revision" : "AB", "testRecipeID":86, "value" : 26.00, "treatment" : "Gas" },
{"model_num" : "ABC", "revision" : "AB", "testRecipeID":86, "value" : 23.75, "treatment" : "Temp" }
]';
$data = json_decode($data);
$result = [];
foreach ($data as $row) {
$a = $row->model_num . '|' . $row->revision . '|' . $row->testRecipeID;
if (! array_key_exists($a, $result)) {
$result[$a] = [
'model_num' => $row->model_num,
'revision' => $row->revision,
'testRecipeID' => $row->testRecipeID
];
}
}