Convert laravel object to array
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.
Related videos on Youtube
Your Friend
Updated on June 13, 2021Comments
-
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 over 9 yearsdid 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 over 9 yearsyes it came from DB..
-
Stefano Groenland over 8 yearsDuplicate question? stackoverflow.com/questions/28658891/…
-
-
Your Friend over 9 yearsi tried both methods.. same error
Call to a member function toArray() on a non-object
-
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 over 9 yearsIam getting data from laravel database ... it uses
std class
...data is exactly same.... -
Mark Baker over 9 yearsIf you're using Laravel properly, it uses model objects and collections, not StdClass objects
-
Jarek Tkaczyk over 9 years@MarkBaker No, only Eloquent does it. If you use simple
Query\Builder
it returns exactly thisarray
ofstdObjects
(by default, depending on the db config) -
Marcin Nabiałek over 9 yearsI 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 over 9 yearsYes, it will affect all the queries for this request unless you revert it. Edited to make it clear.
-
Tim Wong over 8 yearsyou can also edit config/database.php, replace 'fecth' => PDO::FETCH_CLASS to PDO::FETCH_ASSOC
-
Levent Yumerov about 8 yearsThere is an unnecessary quote mark in the end of first line and that breaks the formatting.
-
Benyamin Limanto over 4 yearsThis no longer work on new laravel since version 5.4 They scrap this away