dppn67180 2017-10-27 10:41
浏览 25
已采纳

PHP insenstivite multisort

I'm using this function to sort my multidimensional array:

function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) {
    $sort_col = array();
    foreach ($arr as $key=> $row) {
        $sort_col[$key] = $row[$col];
    }

    array_multisort($sort_col, $dir, $arr);
}

and then call it for example like this:

array_sort_by_column($items, 'name', SORT_DESC);

Now the sorting in general works, but there is one problem: It's case sensitive, so if I have a list of :

  • apple
  • orange
  • Pear
  • Banana

the banana wouldn't come to the second place of the list (or array), but instead to the first place, so first, there are the words with starting capital letters, and then the other ones.

So expected behaviour of the list above would be

  • apple
  • Banana
  • orange
  • Pear

Actual output is:

  • Banana
  • Pear
  • apple
  • orange

What can I change to do this not case sensitive?

  • 写回答

1条回答

  • dsfs64664 2017-10-27 10:45
    关注

    This should do:

    array_multisort($sort_col, $dir|SORT_NATURAL|SORT_FLAG_CASE, $arr);

    From php docs:

    SORT_FLAG_CASE - can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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