php数组合并后排序

//数组合并方法
$dingdan = array_merge($jie_dingdan,$user_dingdan);
            $orderFile = array();
            foreach($dingdan as $vo){
               $orderFile[]=$vo['time'];
               }
//数组合并后排序方法需要的值:按照哪个字段排序,升降排序(SORT_DESC升序  SORT_ASC降序),合并后的数组
array_multisort($orderFile ,SORT_DESC, $dingdan );

$arr_data = array_unique($dingdan);//数组去重

$order = array_values($arr_data );//php 数组索引值重新从0开始递增

实例

$jie_dingdan数组

array(2) {
  [0] => array(8) {
    ["cid"] => int(1)
    ["pid"] => int(0)
    ["cat_name"] => string(12) "接口列表"
    ["item_id"] => int(1)
    ["s_number"] => int(99)
    ["add_time"] => int(1593157603)
    ["level"] => int(0)
    ["list"] => array(4) {
      [0] => array(4) {
        ["id"] => int(4)
        ["item_id"] => int(1)
        ["cat_id"] => int(1)
        ["title"] => string(9) "API列表"
      }
      [1] => array(4) {
        ["id"] => int(5)
        ["item_id"] => int(1)
        ["cat_id"] => int(1)
        ["title"] => string(12) "公共参数"
      }
      [2] => array(4) {
        ["id"] => int(6)
        ["item_id"] => int(1)
        ["cat_id"] => int(1)
        ["title"] => string(12) "数据格式"
      }
      [3] => array(4) {
        ["id"] => int(7)
        ["item_id"] => int(1)
        ["cat_id"] => int(1)
        ["title"] => string(12) "数据签名"
      }
    }
  }

$user_dingdan数组

array(4) {
  [0] => array(5) {
    ["id"] => int(1)
    ["item_id"] => int(1)
    ["cat_id"] => int(0)
    ["title"] => string(6) "前言"
    ["add_time"] => int(1593157545)
  }
  [1] => array(5) {
    ["id"] => int(2)
    ["item_id"] => int(1)
    ["cat_id"] => int(0)
    ["title"] => string(12) "接入流程"
    ["add_time"] => int(1593157569)
  }
  [2] => array(5) {
    ["id"] => int(3)
    ["item_id"] => int(1)
    ["cat_id"] => int(0)
    ["title"] => string(12) "商户授权"
    ["add_time"] => int(1593157586)
  }
  [3] => array(5) {
    ["id"] => int(14)
    ["item_id"] => int(1)
    ["cat_id"] => int(0)
    ["title"] => string(9) "返回码"
    ["add_time"] => int(1593157956)
  }
}

采用上面的代码合并后是

array(5) {
  [0] => array(5) {
    ["id"] => int(1)
    ["item_id"] => int(1)
    ["cat_id"] => int(0)
    ["title"] => string(6) "前言"
    ["add_time"] => int(1593157545)
  }
  [1] => array(5) {
    ["id"] => int(2)
    ["item_id"] => int(1)
    ["cat_id"] => int(0)
    ["title"] => string(12) "接入流程"
    ["add_time"] => int(1593157569)
  }
  [2] => array(5) {
    ["id"] => int(3)
    ["item_id"] => int(1)
    ["cat_id"] => int(0)
    ["title"] => string(12) "商户授权"
    ["add_time"] => int(1593157586)
  }
  [3] => array(8) {
    ["cid"] => int(1)
    ["pid"] => int(0)
    ["cat_name"] => string(12) "接口列表"
    ["item_id"] => int(1)
    ["s_number"] => int(99)
    ["add_time"] => int(1593157603)
    ["level"] => int(0)
    ["list"] => array(4) {
      [0] => array(4) {
        ["id"] => int(4)
        ["item_id"] => int(1)
        ["cat_id"] => int(1)
        ["title"] => string(9) "API列表"
      }
      [1] => array(4) {
        ["id"] => int(5)
        ["item_id"] => int(1)
        ["cat_id"] => int(1)
        ["title"] => string(12) "公共参数"
      }
      [2] => array(4) {
        ["id"] => int(6)
        ["item_id"] => int(1)
        ["cat_id"] => int(1)
        ["title"] => string(12) "数据格式"
      }
      [3] => array(4) {
        ["id"] => int(7)
        ["item_id"] => int(1)
        ["cat_id"] => int(1)
        ["title"] => string(12) "数据签名"
      }
    }
  }
    [4] => array(5) {
    ["id"] => int(14)
    ["item_id"] => int(1)
    ["cat_id"] => int(0)
    ["title"] => string(9) "返回码"
    ["add_time"] => int(1593157956)
  }
}

前台展示

展示里多出一个支付接口分类,是我数据本身就有的,上面为了让代码变少,所以把支持接口代码删除了。
也可以看出,前台展示的顺序就是我们添加文章时的顺序,也正是我们需要的。
php数组合并后排序

评论

大侠名号:   验证暗号: 点击我更换图片

修远兮

在这里记录每一点自己需要的知识

添加创始人微信,一起交流心得

推荐文章RECOMMEND