【问题标题】:Sort multidimensional array according to sub index根据子索引对多维数组进行排序
【发布时间】:2016-06-22 09:44:41
【问题描述】:

我有一个数组项如下。

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

我需要根据以下数组的 VALUE 对数组的field_id INDEX 进行排序

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

OR 到后面的字符串

7,3,4,5,2,6

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

【问题讨论】:

标签: php arrays multidimensional-array


【解决方案1】:

通常的 foreach 会产生预期的结果

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

【讨论】:

  • 谢谢。你回答帮助我解决问题。我只是在你的回答中添加了ksort() 并得到了想要的结果:)
  • 很高兴能帮上忙
猜你喜欢
  • 2015-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多