dongzhao1930 2017-09-20 12:07
浏览 47
已采纳

按值(a)对多维数组进行分组,并按值(b)对每个组进行排序

I have an array containing fruits and vegetables that are all mixed together. I would like to group each item by their type ie 'vegetable' (indicated by value = 1) or 'fruit'(indicated by value = 2) and then have each item positioned in the order assigned to it.

Here is my starting array:

array(3) {
  [0]=>
  array(16) {
    ["name"]=> "apples"
    ["position"]=> "1"
    ["basket"]=> "2"
  }
  [1]=>
  array(16) {
    ["name"]=> "potatoes"
    ["position"]=> "2"
    ["basket"]=> "1"
  }
  [2]=>
  array(16) {
    ["name"]=> "onions"
    ["position"]=> "1"
    ["basket"]=> "1"
  }
  [3]=>
  array(16) {
    ["name"]=> "oranges"
    ["position"]=> "3"
    ["basket"]=> "2"
  }
  [4]=>
  array(16) {
    ["name"]=> "tomatoes"
    ["position"]=> "4"
    ["basket"]=> "1"
  }
  [5]=>
  array(16) {
    ["name"]=> "grapes"
    ["position"]=> "5"
    ["basket"]=> "2"
  }
  [6]=>
  array(16) {
    ["name"]=> "avocado"
    ["position"]=> "2"
    ["basket"]=> "2"
  }
  [7]=>
  array(16) {
    ["name"]=> "bean"
    ["position"]=> "5"
    ["basket"]=> "1"
  }
  [8]=>
  array(16) {
    ["name"]=> "pineapple"
    ["position"]=> "4"
    ["basket"]=> "2"
  }
  [9]=>
  array(16) {
    ["name"]=> "pepper"
    ["position"]=> "3"
    ["basket"]=> "1"
  }
}

Here is the result I am wanting to achieve:

array(3) {
  [0]=>
  array(16) {
    ["name"]=> "onions"
    ["position"]=> "1"
    ["basket"]=> "1"
  }
  [1]=>
  array(16) {
    ["name"]=> "potatoes"
    ["position"]=> "2"
    ["basket"]=> "1"
  }
  [2]=>
  array(16) {
    ["name"]=> "pepper"
    ["position"]=> "3"
    ["basket"]=> "1"
  }
  [3]=>
  array(16) {
    ["name"]=> "tomatoes"
    ["position"]=> "4"
    ["basket"]=> "1"
  }
  [4]=>
  array(16) {
    ["name"]=> "bean"
    ["position"]=> "5"
    ["basket"]=> "1"
  }
  [5]=>
  array(16) {
    ["name"]=> "apples"
    ["position"]=> "1"
    ["basket"]=> "2"
  }
  [6]=>
  array(16) {
    ["name"]=> "avocado"
    ["position"]=> "2"
    ["basket"]=> "2"
  }
  [7]=>
  array(16) {
    ["name"]=> "oranges"
    ["position"]=> "3"
    ["basket"]=> "2"
  }
  [8]=>
  array(16) {
    ["name"]=> "pineapple"
    ["position"]=> "4"
    ["basket"]=> "2"
  }
  [9]=>
  array(16) {
    ["name"]=> "grapes"
    ["position"]=> "5"
    ["basket"]=> "2"
  }
}

In the 'hopeful' result, it can be seen that there are 2 groups for the field 'basket', namely 1 & 2 and therefore the array must be "ordered" that it is 'grouped' in sequence.

Finally, each 'group' item must then be sorted in its own sequence.

This is what I have thus far:

uasort($array, function($a, $b) {return $a['basket'] - $b['basket'];});

Can I adapt this to suit my need?

  • 写回答

3条回答 默认 最新

  • duanliushua5026 2017-09-20 13:04
    关注

    Starting in PHP 5.3, one can use an anonymous function:

    uasort($array, function($a,$b)
        {
            return $a['basket'] - $b['basket'] ?: $a['position'] - $b['position'];
        });
    

    LIVE DEMO

    This is combined with solutions provided by B. Desai & Casimir et Hippolyte

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀