不能使用 Laravel 将 stdClass 类型的对象用作数组

http://img.mukewang.com/60d5950400011d5314590791.jpg

http://img3.mukewang.com/60d5950e0001b86718920721.jpg

我已经制作了功能来显示我对

下面菜单我的控制器代码的所有权限:


<?php

    class PrivilegesController extends Controller

    {

        public function getAllPrivileges()

        {

            $privileges  = DB::table('prev_definition')->orderBy('id', 'asc')->get();

            $privileges  = $this->build_heirarchy($privileges);


            $resultArray = ['status' => true, 'message' => 'Privileges found!', 'data' => $privileges];

            return json_encode($resultArray);

        }


        public function build_heirarchy($result_set, $parent_id = 0)

        {

            $rs = array();

            foreach ($result_set as $row) {


                $row['is_checked'] = 1; // here is error

                if ($row['parent_id'] == $parent_id) {


                    $children = $this->build_heirarchy($result_set, $row['id']);

                    if ($children) {


                        if ($children[0]['type'] == 'menu') {


                            $type = 'submenu';

                        } else {

                            $type = 'permission';

                        }

                        $row[$type] = $children;

                    }

                    $rs[] = $row;

                }

            }

            return $rs;

        }

    }

?>

但是我收到了无法使用类型的对象stdClass作为数组的错误,我很困惑如何才能做到这一点,非常感谢您的帮助!


{

  "data": [

    {

      "created_at": "2019-05-20 15:48:34",

      "deletedAt": null,

      "display_group": "patient",

      "icon": "NULL",

      "id": 297,

      "isDeleted": null,

      "is_checked": 1,

      "parent_id": 0,

      "priv_key": "can_access_patient",

      "priv_title": "Patient",

      "type": "menu",

      "updated_at": "2019-05-20 15:48:34"

    }

  ],

  "message": "Privileges found!",

  "status": true

}

这是我的最终控制器代码,我还与您分享了响应,您可以查看此内容,如果还有任何更改,请告诉我


慕仙森
浏览 280回答 2
2回答

PIPIONE

根据您的建议和评论,我修改了您的代码,一旦检查并让我知道。class PrivilegesController extends Controller{&nbsp; &nbsp; public function getAllPrivileges()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $privileges = DB::table('prev_definition')->orderBy('id', 'asc')->get();&nbsp; &nbsp; &nbsp; &nbsp; // add below line, keep your old code as it is and check&nbsp; &nbsp; &nbsp; &nbsp; $privileges&nbsp; = json_decode(json_encode($privileges), true);&nbsp; &nbsp; &nbsp; &nbsp; $privileges&nbsp; = $this->build_heirarchy($privileges);&nbsp; &nbsp; &nbsp; &nbsp; $resultArray = ['status' => true, 'message' => 'Privileges found!', 'data' => $privileges];&nbsp; &nbsp; &nbsp; &nbsp; return json_encode($resultArray);&nbsp; &nbsp; }&nbsp; &nbsp; public function build_heirarchy($result_set, $parent_id = 0)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $rs = array();&nbsp; &nbsp; &nbsp; &nbsp; foreach ($result_set as $row) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $row['is_checked'] = 1; // here is error&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($row['parent_id'] == $parent_id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $children = $this->build_heirarchy($result_set, $row['id']);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($children) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($children[0]['type'] == 'menu') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $type = 'submenu';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $type = 'permission';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $row[$type] = $children;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $rs[] = $row;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $rs;&nbsp; &nbsp; }}编辑只有父 id 为 0 的记录,因此它只会创建一个父记录,并递归地附加子数据。检查这个。现在,对于父 id 1,它在屏幕截图中有 2 条记录,因此两个元素将附加在父元素中。现在,对于父 id 2,它在屏幕截图中有 4 条记录,因此 4 个元素将附加到 id 为 2 的元素内。考虑到父ID,这将在每个元素的内部进行。随后所有数据将附加到它的每个父级。所以索引只有一个,数据递归地附加到它的每个相关父级。我希望我很清楚。

缥缈止盈

if ($children){&nbsp; &nbsp; if($children[0]->type=='menu')&nbsp; &nbsp; &nbsp; &nbsp; $type = 'submenu';&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; $type = 'permission';&nbsp; &nbsp; $row[$type] = $children;}
打开App,查看更多内容
随时随地看视频慕课网APP