2014-02-25 77 views
-1

我有以下陣列:種類多級陣列的PHP

array(
    array(
      id: 4, 
      name: car, 
      pid: 0 
     ), 
     array(
      id: 5, 
      name: lights, 
      pid: 4 
     ), 
     array(
      id: 6, 
      name: fog, 
      pid: 5 
     ) 
    ), 
    array(
     array(
      id: 1, 
      name: bike, 
      pid: 0 
     ), 
     array(
      id: 2, 
      name: wheel, 
      pid: 1 
     ), 
     array(
      id: 3, 
      name: tire, 
      pid: 2 
     ) 
    ), 
    array(
      id: 7, 
      name: car, 
      pid: 0 
     ), 
     array(
      id: 8, 
      name: lights, 
      pid: 7 
     ), 
     array(
      id: 9, 
      name: brake, 
      pid: 8 
     ) 
    ), 
    array(
      id: 10, 
      name: car, 
      pid: 0 
     ), 
     array(
      id: 11, 
      name: engine, 
      pid: 10 
     ) 
    ), 
) 

這些multidimesnional陣列代表一輛車的規格。在表中,它看起來像:

| car -> lights -> fog | 
| bike -> wheel -> tire | 
| car -> lights -> brake | 
| car -> engine | 

現在我想對數組進行排序,對每名水平。因此,首先,排序第一列,比排序在第二列等等

我們的數組排序應該是這樣的:使用usort

| bike -> wheel -> tire | 
| car -> engine | 
| car -> lights -> brake | 
| car -> lights -> fog | 

我可以排序的第一列:

usort($characteristics, function($a, $b){ 
    return $a[0]['name'] > $b[0]['name']; 
}); 

但offcourse這隻會影響第一列,在這種情況下,數組是這樣的:

| bike -> wheel -> tire | 
| car -> lights -> fog | 
| car -> lights -> brake | 
| car -> engine | 

也有可能有不同的長度(如發動機,有2個值和自行車,有3個)。通常情況下,長度在1到5之間變化。

我寧願不會在/ foreach循環中使用太多,因爲這會減慢我的程序太多。對於長期的問題也很抱歉,但我找不到解決我的問題的簡短方法。

我希望有人可以給我一個簡單的清潔解決方案。

+0

檢查這個http://php.net/manual/en/function.array-multisort.php –

+0

我知道的功能,但我可以我真的不知道應該通過哪個參數來排序我的數組... – JasperV

+0

嘗試使用foreach函數 – vijaykumar

回答

0

我相信這是你在找什麼:

$arr = 
    array(
     array(
      array(
       "id" => 4, 
       "name" => "car", 
       "pid" => 0 
      ), 
      array(
       "id" => 5, 
       "name" => "lights", 
       "pid" => 4 
      ), 
      array(
       "id" => 6, 
       "name" => "fog", 
       "pid" => 5 
      ) 
     ), 
     array(
      array(
       "id" => 1, 
       "name" => "bike", 
       "pid" => 0 
      ), 
      array(
       "id" => 2, 
       "name" => "wheel", 
       "pid" => 1 
      ), 
      array(
       "id" => 3, 
       "name" => "tire", 
       "pid" => 2 
      ) 
     ), 
     array(
      array(
       "id" => 7, 
       "name" => "car", 
       "pid" => 0 
      ), 
      array(
       "id" => 8, 
       "name" => "lights", 
       "pid" => 7 
      ), 
      array(
       "id" => 9, 
       "name" => "brake", 
       "pid" => 8 
      ) 
     ), 
     array(
      array(
       "id" => 10, 
       "name" => "car", 
       "pid" => 0 
      ), 
      array(
       "id" => 11, 
       "name" => "engine", 
       "pid" => 10 
      ) 
     ) 
    ); 

for ($i = 0; $i < count($arr); $i++) { 
    foreach ($arr[$i] as $a) { 
     foreach($a as $key => $value) { 
      if ($key == 'name') { 
       $newArr[$i][] = $value; 
      } 
     } 
    } 
} 

usort($newArr, function($a, $b) { 
    return $a[0] > $b[0]; 
}); 

echo "<pre>", print_r($newArr), "</pre>"; 
+0

不完全......它完美的工作排序名稱,但我失去了額外的信息(身份證,PID等),它在eachother巢,嵌套3循環,它與THES例如數據,但不是與我的數據(這是更大的) – JasperV

+0

在OP中提到這些細節? – Tomanow