douyan1972 2013-08-28 23:07
浏览 69
已采纳

PHP Array_multisort状态

I'm trying to multisort a DB array based on the users status. Those with Status = 1 go at the top, those with Status = 0 go at the bottom of the array. I thought I had it working but it just stopped today with the addition of new rows to the DB.

uasort($ven, function ($a, $b) { return $a['v_status'] == '1' ? false : true; });

It's a simple DB array from MySQL:

Array (
 [0] => array(
   [name] => '',
   [v_status] => 0
 [1] => array(
   [name] => '',
   [v_status] => 1
)
  • 写回答

2条回答 默认 最新

  • dongpo1203 2013-09-03 09:13
    关注

    As mentioned in comments to my other answer, splitting the array into active/inactive arrays could be a better solution than sorting.

    $items = array(
        array('name' => 'active1', 'active' => '1'),
        array('name' => 'inactive1', 'active' => '0'),
        array('name' => 'active2', 'active' => '1'),
        array('name' => 'inactive2', 'active' => '0'),
        array('name' => 'inactive3', 'active' => '0'),
        array('name' => 'active3', 'active' => '1'),
        array('name' => 'inactive4', 'active' => '0'),
    );
    
    $active = array_filter($items, function($item){ return $item['active'] == '1'; });
    echo '<pre>' . print_r($active,true);
    
    // You could filter again here, not sure which would be quicker, 
    // but my guess would be the array_diff method (which also ensures
    // that no items get filtered out by both filters)
    $inactive = array_diff_key($items, $active);
    echo '<pre>' . print_r($inactive,true);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要