Please try this. this will return array of objects.

$result = json_decode(json_encode($data, true));

*Updated

if you want to convert all nested properties to the array, try this.

$result = json_decode(json_encode($data, true), true);
Answer from Alex on Stack Overflow
Discussions

php - Laravel. Cannot use object of type StdClass as array after toArray() convert - Stack Overflow
No, get() returns a collection of objects, not an array 2018-01-10T13:09:39.233Z+00:00 ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... This question is in a collective: a subcommunity defined by tags with relevant content and experts. ... Community Asks Sprint Announcement – January 2026: Custom site-specific badges! Stack Overflow chat opening up to all users in January; Stack Exchange chat... ... 0 Laravel Object of class stdClass ... More on stackoverflow.com
🌐 stackoverflow.com
January 10, 2018
PHP Error: Cannot use object of type stdClass as array in Laravel - Stack Overflow
Stack Overflow chat opening up to all users in January; Stack Exchange chat... ... 0 Laravel Object of class stdClass could not be converted to string. More on stackoverflow.com
🌐 stackoverflow.com
php - Cannot use object of type stdClass as array in Laravel - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... PHP Fatal error: Cannot use object of type stdClass as array in C:\xampp\htdocs\enginepoker2\storage\framework\views\10cbbd076bede57a96e59c43af0e1b9e022b4a69.php on line 115 More on stackoverflow.com
🌐 stackoverflow.com
php - Convert laravel object to array - Stack Overflow
Object of stdClass has been converted to array. ... i tried both methods.. same error Call to a member function toArray() on a non-object 2014-10-03T06:41:39.56Z+00:00 ... @user3436481 Have you tried using only (array) as I showed in my answer? You should edit your question and show how exactly you get data and how you use array conversion 2014-10-03T06:44:15.4Z+00:00 ... Iam getting data from laravel ... More on stackoverflow.com
🌐 stackoverflow.com
October 4, 2014
🌐
Laracasts
laracasts.com › discuss › channels › general-discussion › cannot-use-object-of-type-stdclass-as-array-1
Cannot use object of type stdClass as array
In Laravel 5.4.21 DB:: query's will return stdObject class objects, not Eloquent models. So in blade $role['name'] should be $role->name as you cannot treat an stdObject as an array (the error is pretty explanatory in this case ;) ) the reason why the first one IS in fact working is only due to the fact that Eloquent models implement ArrayAccess by default which allows you to treat them as if they were a plain array instead of an class object.
Find elsewhere
🌐
Strangebuzz
strangebuzz.com › home › snippets › converting a php stdclass object into an array
Converting a PHP stdClass object into an array | Strangebuzz
*/ trait Snippet311Trait { public function snippet311(): void { $stdClass = new \stdClass(); $stdClass->foo = 'bar'; $stdClass->object_property = new \stdClass(); $stdClass->object_property->foo2 = 'bar2'; echo '----- Nominal case OK ✅'.PHP_EOL; echo '> original object:'.PHP_EOL; $array = json_decode((string) json_encode($stdClass), true); var_dump($stdClass); echo '> as array:'.PHP_EOL; var_dump($array); echo PHP_EOL; // With a cast, it doesn't work as the object_property is not converted into an array echo '----- cast case, does not work ❌'.PHP_EOL; var_dump((array) $stdClass); // That's it! 😁 } } Run this snippet More on Stackoverflow Read the doc Random snippet · Work with me! ... Did you like this post? You can help me back in several ways: (use the "reply" link on the right to comment or to contact me )
Top answer
1 of 2
1

As per your suggestions and comments I modified your code, once check and let me know.

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

EDIT

  1. There is only record with parent id 0, so it will create only one parent record, and recursively it will have appended child data. Check this.
  2. Now for parent id 1 it has 2 records in screenshot, so two elements will appended inside parent single most element.
  3. Now for parent id 2 it has 4 records in screenshot, so 4 elements will be appended inside element with id 2.
  4. This will go on inside to inside of every element considering parent id.
  5. Subsequently all data will be appended to every parent of it. So index is only one and data is appended recursively to its every relevant parent.

I hope I am clear.

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

    $row[$type] = $children;
}
🌐
GitHub
github.com › laravel › framework › issues › 23966
[5.4] Feature Request: need toObject (stdClass array) like toArray in Collection & Model · Issue #23966 · laravel/framework
April 22, 2018 - Laravel Version: 5.4.36 PHP Version: 7.1 Database Driver & Version: None Description: This is a feature request. Steps To Reproduce: None
Author: laravel
🌐
Danyal
danyal.dk › home › php: how to convert stdclass to array
PHP: How to convert stdclass to array - Full Stack Programmer
December 7, 2020 - $chairArray = object_to_array($chair); function objectToArray ($object) { if(!is_object($object) && !is_array($object)){ return $object; } return array_map('object_to_array', (array) $object); } Post Views: 3,797 · Author: Danyal · I'm a skilled programmer specializing in Vue.js/Nuxt.js for front-end development and PHP Laravel for back-end solutions.
🌐
Laracasts
laracasts.com › discuss › channels › code-review › how-to-remove-stdobject-when-converting-collection-to-array
How to remove stdObject when converting collection to array?
array:2 [ 0 => {#2993 +"item_id": 11 +"item_code": "A1B2C3" } 1 => {#2994 +"item_id": 11343 +"item_code": "A1B2C4" } ] } But I can't access it normally for example in a for each loop: foreach ($data as $item) { echo ($item['item_id']) } I get Object of class stdClass could not be converted to string" or there's a way to do it without converting to array (so only after using ->get()) Sinnbeck ·
🌐
Laracasts
laracasts.com › discuss › channels › eloquent › convert-stdclass-array-to-model-object
Convert stdClass array to Model object
I just posted the same thing on another discussion but it's at the bottom of two long pages of comments and might be easily missed. I'm having to use the DB::table() function to retrieve a custom ordered set of records. This works fine except it returns a simple array and I need the Model I defined in Laravel because some actions in the blade this is passed to need those pred