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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器