PHP 二维数组根据某一元素值筛选

  • 2019 年 12 月 23 日
  • 筆記

今天遇到了一个问题,就是用框架封装的方法取出数据库中的数据后,显示的数据是一个二维数组,所得到的数值在二维数组中得到,一维数组是一个索引,本想取出数据交给前台进行筛选显示数据,但是后期数据量比较大,前端解析必定耗时过久,严重影响其用户体验,今天从网上找了一下教程,总结了一下,如下面思路见解:

原始数据

array(3) {    [0]=>    array(13) {      ["Order_id"]=>      int(1071)      ["User_id"]=>      int(1)      ["Order_Status"]=>      int(14)      ["Robot_id"]=>      string(6) "ZR1016"    }    [1]=>    array(13) {      ["Order_id"]=>      int(1072)      ["User_id"]=>      int(1)      ["Order_Status"]=>      int(14)      ["Robot_id"]=>      string(6) "ZR1016"    }    [2]=>    array(13) {      ["Order_id"]=>      int(1115)      ["User_id"]=>      int(1)      ["Order_Status"]=>      int(12)      ["Robot_id"]=>      string(6) "ZR1016"    }  }

数据结构

/**   * $data 二维数组数据   * $value 定义接受数组进行筛选接受   */  $value = [];  foreach($data as $key=>$value){      $result[$value['Order_Status']][] = $value;  }

处理数据

array(2) {    [12]=>    array(1) {      [0]=>      array(13) {        ["Order_id"]=>        int(1115)        ["User_id"]=>        int(1)        ["Order_Status"]=>        int(12)        ["Robot_id"]=>        string(6) "ZR1016"      }    }    [14]=>    array(2) {      [0]=>      array(13) {        ["Order_id"]=>        int(1071)        ["User_id"]=>        int(1)        ["Order_Status"]=>        int(14)        ["Robot_id"]=>        string(6) "ZR1016"      }      [1]=>      array(13) {        ["Order_id"]=>        int(1072)        ["User_id"]=>        int(1)        ["Order_Status"]=>        int(14)        ["Robot_id"]=>        string(6) "ZR1016"      }    }  }