PHP while循环输出二维数组 方法

今天在用Mysqli的时候,用 while ”$stmt -> fetch()“一直想生成一个二维数组,那样前端就好调用了,经过不断测试,最后解决代码如下:

 public function user_group_purview_select($mysqli,$user_group_id ,$id='',$type='',$action=''){
        //通过映射表 用户管理ID, 查询 用户管理组权限 id action
        $sql_select = "SELECT `id`,`type`,`action` FROM `user_group_purview` WHERE `id` in(SELECT `user_group_purview_id` FROM `user_group_purviewmap` WHERE `user_group_id` = ?) ORDER BY `id` ASC";
        $stmt=$mysqli->prepare($sql_select);
        $stmt->bind_param('i',$user_group_id);
        $stmt->execute();
        $stmt -> bind_result ($id,$type,$action);//这里参数跟你查询的字段显示个数需要对应起来!
        $arr =[];
        $arr2 =[];
        while ( $stmt -> fetch ()) {
            $arr['id'] = $id;
            $arr['type'] = $type;
            $arr['action'] = $action;
            $arr2[] = $arr;
        }
        /*这里循环得到一个二维数组*/
        return $arr2;
    }

 

最后的效果如下:

array(8) {
  [0]=>
  array(3) {
    ["id"]=>
    int(94)
    ["type"]=>
    string(8) "category"
    ["action"]=>
    string(15) "["select-list"]"
  }
  [1]=>
  array(3) {
    ["id"]=>
    int(95)
    ["type"]=>
    string(7) "article"
    ["action"]=>
    string(15) "["select-list"]"
  }
  [2]=>
  array(3) {
    ["id"]=>
    int(96)
    ["type"]=>
    string(5) "image"
    ["action"]=>
    string(15) "["select-list"]"
  }
  [3]=>
  array(3) {
    ["id"]=>
    int(97)
    ["type"]=>
    string(3) "tag"
    ["action"]=>
    string(15) "["select-list"]"
  }
  [4]=>
  array(3) {
    ["id"]=>
    int(98)
    ["type"]=>
    string(7) "comment"
    ["action"]=>
    string(15) "["select-list"]"
  }
  [5]=>
  array(3) {
    ["id"]=>
    int(99)
    ["type"]=>
    string(4) "user"
    ["action"]=>
    string(15) "["select-list"]"
  }
  [6]=>
  array(3) {
    ["id"]=>
    int(100)
    ["type"]=>
    string(9) "usergroup"
    ["action"]=>
    string(15) "["select-list"]"
  }
  [7]=>
  array(3) {
    ["id"]=>
    int(101)
    ["type"]=>
    string(16) "usergrouppurview"
    ["action"]=>
    string(82) "["select-list","update","insert","delete","select","left-sidebar","manual-review"]"
  }
}
    A+
发布日期:2020年10月02日 10:52:38  所属分类:PHP
最后更新时间:2020-10-02 10:52:38
付杰
  • ¥ 99.0元
  • 市场价:99.0元
  • ¥ 199.0元
  • 市场价:399.0元
  • ¥ 6.8元
  • 市场价:8.8元
  • ¥ 99.9元
  • 市场价:299元

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: