2013-10-22 69 views
1

我有這樣的查詢:如何用另一個結構格式化關聯數組?

select truck, oil_type, km_min, km_max from trucks; 

我用笨並與$query->result_array()功能,所以它在這個結構的關聯數組裝:

/* 

array(
    truck 
    oil_type 
    km_min 
    km_max 
) 

*/ 

$arr[0]["truck"]  = 2; 
$arr[0]["oil_type"] = 2; 
$arr[0]["km_min"] = 345; 
$arr[0]["km_max"] = 567; 
$arr[1]["truck"]  = 2; 
$arr[1]["oil_type"] = 4; 
$arr[1]["km_min"] = 234; 
$arr[1]["km_max"] = 867; 
$arr[2]["truck"]  = 1; 
$arr[2]["oil_type"] = 2; 
$arr[2]["km_min"] = 545; 
$arr[2]["km_max"] = 867; 
$arr[3]["truck"]  = 4; 
$arr[3]["oil_type"] = 3; 
$arr[3]["km_min"] = 45; 
$arr[3]["km_max"] = 567; 

然後,我想重組它的卡車由ID分組,如下所示:

/* 

trucks - array(
    truck 
    truck_data - array(
     oil_type 
     km_min 
     km_max 
    ) 
) 

*/ 

$arr["truck"][0]= 2; 
$arr["truck"][0]["truck_data"][0]["oil_type"] = 2; 
$arr["truck"][0]["truck_data"][0]["km_min"] = 345; 
$arr["truck"][0]["truck_data"][0]["km_max"] = 567; 
$arr["truck"][0]["truck_data"][1]["oil_type"] = 4; 
$arr["truck"][0]["truck_data"][1]["km_min"] = 234; 
$arr["truck"][0]["truck_data"][1]["km_max"] = 867; 
$arr["truck"][1]= 1; 
$arr["truck"][1]["truck_data"][0]["oil_type"] = 2; 
$arr["truck"][1]["truck_data"][0]["km_min"] = 545; 
$arr["truck"][1]["truck_data"][0]["km_max"] = 867; 
$arr["truck"][2]= 4; 
$arr["truck"][2]["truck_data"][0]["oil_type"] = 3; 
$arr["truck"][2]["truck_data"][0]["km_min"] = 45; 
$arr["truck"][2]["truck_data"][0]["km_max"] = 567;  

我以爲像下面的代碼想:

$res = $query->result_array(); 
$cnt_total = count($res); 
$y = 0; 

for ($x=0; $x < $cnt_total -1 ; $x++) { 

    $truck = $res[$x]["truck"]; 
    $trucks["truck"][$y] = $truck; 

    $q = $x + 1; 
    $i = 0; 

    do{ 
     $trucks[$y][$i]["oil_type"] = $res[$q]["oil_type"]; 
     $trucks[$y][$i]["km_min"] = $res[$q]["km_max"]; 
     $trucks[$y][$i]["km_max"] = $res[$q]["km_max"]; 

     $q++; 
     $i++; 
     if ($q <= $cnt_total) break; 
    } while ( $truck === $res["truck"][$q]); 

    $y++; 

} 

但是不能正常工作...我太遠瞭解決方案?表現在這種特殊情況下對我來說很重要。

有一個phpfiddle在那裏你可以嘗試: http://phpfiddle.org/main/code/67j-ui5

任何想法,提示或建議可以理解的,如果你需要更多的信息,讓我知道,我會編輯的帖子。

回答

1

作爲預期結果提供的內容有點含糊不清。這可能會給你所要求的。

$trucks=array(); 
$res = $query->result_array(); // assuming this is actually several trucks 

foreach ($res as $r){ 
    // set up the array from the data without writing out every column manually 
    $truck=array(
     'truck'=>$r['truck'], 
     'truck_data'=>$r, 
    ); 

    // remove the bit you wanted separately as 'truck' from 'truck_data' 
    unset($truck['truck_data']['truck']); 

    // push into $trucks 
    $trucks[]=$truck;  
} 
+0

謝謝你的回答,我已經更新了這個問題,檢查了一下,恐怕我還沒有足夠清楚。 – harrison4

+0

好的。試試我的,這並不遙遠。你應該能夠得到你想要的。你的問題仍然有點模糊。 '$ arr [「truck」] [0] [「truck_data」] [0] [「km_max」] = 567;'後跟'$ arr [「truck」] [0] [「truck_data」] [1] [ 「oil_type」] = 4;'我不明白。 – Popnoodles

1

試試這個:

$res = $query->result_array(); 

$trucks = array(); 
foreach ($res as $row) { 
    $truck["truck"] = $row["truck"]; 
    $truck["truck_data"] = array(
     'oil_type' => $row["oil_type"], 
     'km_min' => $row["km_min"], 
     'km_max' => $row["km_max"], 
    ); 
    $trucks[] = $truck; 
} 

var_dump($trucks); 
0

這是你需要什麼?轉換結果存儲在$結果

$result = array(); 
foreach($query->result_array() as $truck) 
{ 
    $new_truck = array(); 
    $new_truck['truck'] = $truck['truck']; 
    $new_truck['truck_data'] = array(); 
    $new_truck['truck_data']['oil_type'] = $truck['oil_type']; 
    $new_truck['truck_data']['km_min'] = $truck['km_min']; 
    $new_truck['truck_data']['km_max'] = $truck['km_max']; 
    array_push($result, $new_truck); 
} 
相關問題