使用Laravel无法将stdClass类型的对象用作数组

时间:2019-05-20 12:34:26

标签: php arrays laravel multidimensional-array

enter image description here enter image description here我已经启用了显示菜单所有权限的功能
我的控制器代码如下:

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


                   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);

}

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; // keys to object can only set using curly braces if variable
            }
            $rs[] = $row;
        }
    }
    return $rs;
}

}

这是我最终的控制器代码,我也与您分享了答复,您可以研究一下并让我知道是否还有任何更改

2 个答案:

答案 0 :(得分:2)

根据您的建议和意见,我修改了您的代码,请检查并告知我。

class PrivilegesController extends Controller
{
    public function getAllPrivileges()
    {
        $privileges = DB::table('prev_definition')->orderBy('id', 'asc')->get();
        // add below line, keep your old code as it is and check
        $privileges  = json_decode(json_encode($privileges), true);
        $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;
    }
}

编辑

  1. 只有父ID为0的记录,因此它将仅创建一个父记录,并且递归地将附加子数据。选中this
  2. 现在对于父ID 1,它在屏幕快照中有2条记录,因此两个元素将追加到父中的most个元素中。
  3. 现在对于父ID 2,它在屏幕快照中有4条记录,因此ID为2的元素内将附加4个元素。
  4. 考虑到父元素,这将在每个元素的内部进行 ID。
  5. 随后,所有数据将附加到其每个父级。因此索引只是一个索引,数据递归地附加到其每个相关的父级。

我希望我很清楚。

答案 1 :(得分:1)

if ($children)
{
    if($children[0]->type=='menu')
        $type = 'submenu';
    else
        $type = 'permission';

    $row[$type] = $children;
}