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"      }    }  }