如何使用其他结构格式化关联数组?

时间:2013-10-22 00:14:20

标签: php arrays algorithm codeigniter associative-array

我有这样的查询:

select truck, oil_type, km_min, km_max from trucks;

我正在使用codeigniter并使用$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

任何想法,提示或建议都将受到赞赏,如果您需要更多信息,请告诉我,我将编辑该帖子。

3 个答案:

答案 0 :(得分: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;    
}

答案 1 :(得分: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);

答案 2 :(得分:0)

这是你需要的吗?转换结果存储在$ result

$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);
}