phplaravel

Laravel - How to convert Stdclass object to Array


I'm facing this problem when use database in Laravel. How can i convert that to Array the most simpletest?

$data = DB::table('users')->get();

Solution

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