【问题标题】:How to format an associative array with another structure?如何用另一个结构格式化关联数组?
【发布时间】:2013-10-22 00:14:19
【问题描述】:

我有一个这样的查询:

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

我们将不胜感激任何想法、提示或建议,如果您需要更多信息,请告诉我,我会编辑帖子。

【问题讨论】:

    标签: php arrays algorithm codeigniter associative-array


    【解决方案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】:

      您提供的预期结果有点模棱两可。这可能会满足您的要求。

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

      【讨论】:

      • 感谢您的回答,我已经更新了问题,请检查一下,恐怕之前我还不够清楚。
      • 好的。试试我的,不远了。你应该能够得到你想要的。你的问题仍然有点模糊。 $arr["truck"][0]["truck_data"][0]["km_max"] = 567; 后面跟着$arr["truck"][0]["truck_data"][1]["oil_type"] = 4; 我不明白。
      【解决方案3】:

      这是你需要的吗?转换结果存储在$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);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-10
        • 1970-01-01
        • 1970-01-01
        • 2010-11-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多