2016-09-01 82 views
1

樞軸陣列我有以下幾點:如何在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中這樣做,以便我可以處理我擁有的大量數據。我有這個基礎上,我發現了另一個問題,但它不起作用,它返回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

你說喲知道如何在JS做到這一點 - 告訴我們,轉換功能,PHP應該是沒有問題 – mazedlx

+0

@mazedlx我加入我該怎麼辦它在JS謝謝 – cocopan

+0

你保證輸入'$ data'數組是按修訂版還是testRecipeId(它們的唯一性似乎是多餘的)排序? –

回答

0

這是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 
     ]; 
    } 
} 
0

如果您需要的結構,你可以用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)); 
+0

謝謝我實際上需要能夠結合記錄在一個數組中,這意味着,而不是有3行測試食譜ID 85我想只有一個所有的治療和價值 – cocopan

+0

對不起,如果我誤解了你的問題,現在我明白了;所以,你是否發送來自JS的var數據,你希望它在PHP中混合?或者你想從你的JS發送一行嗎? –

+0

不用擔心,謝謝你試圖幫助我欣賞它。 var數據只是我得到的MSSQL結果的簡化版本,然後我將這個結果在php中進行比較,並將比較結果發送給JS,並在JS中創建PIVOT。這種方法的問題是,我有很多數據和比較查殺處理時間,從來沒有把它交給我的JS,所以我想在我得到查詢結果後在PHP中進行數據透視,然後比較它,並將它發送給js已經擺動 – cocopan