首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在php中创建透视数组

如何在php中创建透视数组
EN

Stack Overflow用户
提问于 2016-09-02 01:04:30
回答 2查看 4.6K关注 0票数 1

我有以下几点:

代码语言:javascript
复制
   $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' }
];

我想以这样的方式结束:

代码语言:javascript
复制
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

代码语言:javascript
复制
$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中做的事情

代码语言:javascript
复制
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;
}, {});
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-02 13:59:02

这是PHP中的JS函数

代码语言:javascript
复制
<?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
        ];
    }
}
票数 0
EN

Stack Overflow用户

发布于 2016-09-02 01:11:28

如果您需要这种结构,可以尝试使用JSON Encode

代码语言:javascript
复制
<?php
  $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

  echo json_encode($arr);
?>

以下哪项输出:

代码语言:javascript
复制
{"a":1,"b":2,"c":3,"d":4,"e":5}

如果你需要它是一个数组,使用:

代码语言:javascript
复制
echo json_encode(array($arr));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39277227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档