Convert laravel object to array

206,158

Solution 1

UPDATE since version 5.4 of Laravel it is no longer possible.

You can change your db config, like @Varun suggested, or if you want to do it just in this very case, then:

DB::setFetchMode(PDO::FETCH_ASSOC);

// then
DB::table(..)->get(); // array of arrays instead of objects

// of course to revert the fetch mode you need to set it again
DB::setFetchMode(PDO::FETCH_CLASS);

For New Laravel above 5.4 (Ver > 5.4) see https://laravel.com/docs/5.4/upgrade fetch mode section

Event::listen(StatementPrepared::class, function ($event) {
    $event->statement->setFetchMode(...);
});

Solution 2

foreach($yourArrayName as $object)
{
    $arrays[] = $object->toArray();
}
// Dump array with object-arrays
dd($arrays);

Or when toArray() fails because it's a stdClass

foreach($yourArrayName as $object)
{
    $arrays[] =  (array) $object;
}
// Dump array with object-arrays
dd($arrays);

Not working? Maybe you can find your answer here:

Convert PHP object to associative array

Solution 3

Just in case somebody still lands here looking for an answer. It can be done using plain PHP. An easier way is to reverse-json the object.

function objectToArray(&$object)
{
    return @json_decode(json_encode($object), true);
}

Solution 4

this worked for me:

$data=DB::table('table_name')->select(.......)->get();
$data=array_map(function($item){
    return (array) $item;
},$data);

or

$data=array_map(function($item){
    return (array) $item;
},DB::table('table_name')->select(.......)->get());

Solution 5

You can also get all the result always as array by changing

// application/config/database.php

'fetch' => PDO::FETCH_CLASS,
 // to
'fetch' => PDO::FETCH_ASSOC,

Hope this will help.

Share:
206,158

Related videos on Youtube

Your Friend
Author by

Your Friend

Updated on June 13, 2021

Comments

  • Your Friend
    Your Friend almost 3 years

    Laravel output:

    Array
    (
        [0] = stdClass Object
        (
            [ID] = 5
    
        )
    
        [1] = stdClass Object
        (
            [ID] = 4
    
        )
    
    )
    

    I want to convert this into normal array. Just want to remove that stdClass Object. I also tried using ->toArray(); but I get an error:

    Call to a member function toArray() on a non-object.

    How can I fix this?

    Functionalities have been Implemented on http://www.srihost.com

    • Kevin
      Kevin over 9 years
      did this object came from the DB? i haven't used laravel, but maybe the have an API that results an associative array instead of objects. no need to convert the whole object and then transfer them into another array
    • Your Friend
      Your Friend over 9 years
      yes it came from DB..
    • Stefano Groenland
      Stefano Groenland over 8 years
  • Your Friend
    Your Friend over 9 years
    i tried both methods.. same error Call to a member function toArray() on a non-object
  • Marcin Nabiałek
    Marcin Nabiałek over 9 years
    @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
  • Your Friend
    Your Friend over 9 years
    Iam getting data from laravel database ... it uses std class ...data is exactly same....
  • Mark Baker
    Mark Baker over 9 years
    If you're using Laravel properly, it uses model objects and collections, not StdClass objects
  • Jarek Tkaczyk
    Jarek Tkaczyk over 9 years
    @MarkBaker No, only Eloquent does it. If you use simple Query\Builder it returns exactly this array of stdObjects (by default, depending on the db config)
  • Marcin Nabiałek
    Marcin Nabiałek over 9 years
    I think you should add to your question this will change fetch mode for all queries not only for this one. But nice to know it can be also changed this way
  • Jarek Tkaczyk
    Jarek Tkaczyk over 9 years
    Yes, it will affect all the queries for this request unless you revert it. Edited to make it clear.
  • Tim Wong
    Tim Wong over 8 years
    you can also edit config/database.php, replace 'fecth' => PDO::FETCH_CLASS to PDO::FETCH_ASSOC
  • Levent Yumerov
    Levent Yumerov about 8 years
    There is an unnecessary quote mark in the end of first line and that breaks the formatting.
  • Benyamin Limanto
    Benyamin Limanto over 4 years
    This no longer work on new laravel since version 5.4 They scrap this away