根据子索引对多维数组进行排序

时间:2016-06-22 09:44:41

标签: php arrays multidimensional-array

我有一个数组项目。

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
        )
)

我需要根据以下数组

对数组' s field_id INDEX进行排序
Array
(
    [0] => 7
    [1] => 3
    [2] => 4
    [3] => 5
    [4] => 2
    [5] => 6
)

或跟随字符串

7,3,4,5,2,6

我尝试使用uksort()uasort()对数组进行排序。

1 个答案:

答案 0 :(得分:2)

通常的foreach取得了理想的结果

$index = array_flip([7,3,4,5,2,6]);
foreach($arr as $item) 
   $res[$index[$item['field_id']]] = $item;
相关问题