php二维数组根据某个字段排序的排序(商品按销量或其他字段排序)

在这里插入图片描述

// 连接数据库
  require './includes/connect.class.php';
  $conn = Connect::getInstance();
  // 获取数据
  $res = $conn->fetchAll("SELECT * FROM `b_sale` UNION ALL SELECT * FROM `b_lend` ");

  if (isset($_GET['action']) && $_GET['action'] == 'sale') {
    //  按销量排序
    array_multisort(array_column($res, 'num'), SORT_ASC, $res);
  } elseif (isset($_GET['action']) && $_GET['action'] == 'price') {
    //  按价格排序
    array_multisort(array_column($res, 'price'), SORT_ASC, $res);
  }

在这里插入图片描述

posted @ 2022-04-02 09:47  coderwcb  阅读(55)  评论(0)    收藏  举报