如何在 php 中创建主元数组

how to create a pivot array in 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' }
];

我想以这样的方式结束:

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 中执行此操作,以便我可以处理我拥有的大量数据.我有这个基于我发现的另一个问题,但它不起作用 returns a 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;
}, {});

如果你需要这个结构,你可以试试 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));

这是您在 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
        ];
    }
}

因为前面的答案都没有提供所需的输出,所以我不得不回答这个关键问题。

  1. 根据 3 个标识列值创建一个临时字符串。
  2. 在对相关行数据进行分组时使用该字符串作为第一级键。
  3. 空合并赋值运算符使您无需调用 isset() 来检查之前是否遇到过该组。如果是第一次遇到,把三个核心元素存到行里。
  4. 无条件地将动态键值推送到组的行中。 5 完成迭代后,使用 array_values().
  5. 清除临时键

代码:(Demo)

$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.0,  'treatment' => 'Gas'],
    ['model_num' => 'ABC', 'revision' => 'AB', 'testRecipeID' => 86, 'value' => 23.75, 'treatment' => 'Temp']
];
$result = [];
foreach ($data as $row) {
    $compositeKey = "{$row['model_num']}-{$row['revision']}-{$row['testRecipeID']}";
    $result[$compositeKey] ??= [
        'model_num' => $row['model_num'],
        'revision' => $row['revision'],
        'testRecipeID' => $row['testRecipeID']
    ];
    $result[$compositeKey][$row['treatment']] = $row['value'];
}
var_export(array_values($result));

输出:

array (
  0 => 
  array (
    'model_num' => 'ABC',
    'revision' => 'AA',
    'testRecipeID' => 85,
    'Pressure' => 31.25,
    'Gas' => 31.25,
    'Temp' => 33.12,
    'Current' => 25.87,
  ),
  1 => 
  array (
    'model_num' => 'ABC',
    'revision' => 'AB',
    'testRecipeID' => 86,
    'Pressure' => 26.63,
    'Gas' => 26.0,
    'Temp' => 23.75,
  ),
)