2016-06-22 75 views
-2

我有一個數組項如下。根據子索引對多維數組排序

Array 
(
    [0] => Array 
     (
      [0] => 2 
      [field_id] => 2 
      [1] => Photometric Interpretation 
      [title] => Photometric Interpretation 
      [2] => text 
      [field_type] => text 
     ) 

    [1] => Array 
     (
      [0] => 3 
      [field_id] => 3 
      [1] => Make 
      [title] => Make 
      [2] => text 
      [field_type] => text 
     ) 

    [2] => Array 
     (
      [0] => 4 
      [field_id] => 4 
      [1] => Model 
      [title] => Model 
      [2] => text 
      [field_type] => text 
     ) 

    [3] => Array 
     (
      [0] => 5 
      [field_id] => 5 
      [1] => Strip Offsets 
      [title] => Strip Offsets 
      [2] => text 
      [field_type] => text 
     ) 

    [4] => Array 
     (
      [0] => 6 
      [field_id] => 6 
      [1] => Samples Per Pixel 
      [title] => Samples Per Pixel 
      [2] => text 
      [field_type] => text 
     ) 

    [5] => Array 
     (
      [0] => 7 
      [field_id] => 7 
      [1] => Rows Per Strip 
      [title] => Rows Per Strip 
      [2] => text 
      [field_type] => text 
     ) 

    [6] => Array 
     (
      [0] => 8 
      [field_id] => 8 
      [1] => Software 
      [title] => Software 
      [2] => text 
      [field_type] => text 
     ) 

    [7] => Array 
     (
      [0] => 9 
      [field_id] => 9 
      [1] => Exposure Time 
      [title] => Exposure Time 
      [2] => text 
      [field_type] => text 
     ) 
) 

我需要根據這個以下數組的價值

Array 
(
    [0] => 7 
    [1] => 3 
    [2] => 4 
    [3] => 5 
    [4] => 2 
    [5] => 6 
) 

或以下字符串

7,3,4,5,2,6 

我曾嘗試使用uksort()uasort()排序數組排序上述陣列的field_id INDEX。

+2

的可能的複製[PHP - 由另一個陣列排序多維數組](http://stackoverflow.com/questions/7814420/php-sort-multi-由另一個陣列組成的三維陣列) - 3.3k代表應該認爲你知道告訴我們你嘗試過什麼。 – Epodax

+0

@Epodax我已經嘗試過這個鏈接,但不起作用,所以我打開新的問題。 – Hassaan

+0

[我真的需要鏈接這個嗎?](http://stackoverflow.com/help/how-to-ask) - 告訴我們你試過的東西,它爲什麼不起作用,你得到了什麼錯誤等等。 – Epodax

回答

2

常用的foreach使得所期望的結果

$index = array_flip([7,3,4,5,2,6]); 
foreach($arr as $item) 
    $res[$index[$item['field_id']]] = $item; 
+0

謝謝。你回答幫助我解決問題。我只是添加'ksort()'給你答案,並得到了預期的結果:) – Hassaan

+0

很高興可以幫助 – splash58