<?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;
}
}
这是我最终的控制器代码,我也与您分享了答复,您可以研究一下并让我知道是否还有任何更改
答案 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 :(得分:1)
if ($children)
{
if($children[0]->type=='menu')
$type = 'submenu';
else
$type = 'permission';
$row[$type] = $children;
}