dream989898 2015-12-17 07:21
浏览 14
已采纳

如何以升序形式对多维数组进行排序?

Here is the array. What I want is I wanna sort the array on the basis on 'income_difference' in ascending order. If possible, I just want 3 data with least income difference.

array:5 [▼
  0 => array:4 [▼
    "month" => "11"
    "income_times" => 2
    "income_amount" => 52300
    "income_difference" => 49000
  ]
  1 => array:4 [▼
    "month" => "10"
    "income_times" => 1
    "income_amount" => 50000
    "income_difference" => 46700
  ]
  2 => array:4 [▼
    "month" => "09"
    "income_times" => 1
    "income_amount" => 5000000
    "income_difference" => 4996700
  ]
  3 => array:4 [▼
    "month" => "08"
    "income_times" => 1
    "income_amount" => 50000
    "income_difference" => 46700
  ]
  4 => array:4 [▼
    "month" => "06"
    "income_times" => 1
    "income_amount" => 5200
    "income_difference" => 1900
  ]
]
  • 写回答

2条回答 默认 最新

  • doudu2591 2015-12-17 07:57
    关注

    Use usort:

    usort($data, function($a, $b) {
        return $a['income_difference'] - $b['income_difference'];
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?