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
Answer from Maurice on Stack Overflowforeach($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
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(...);
});
laravel - How to convert an object properties into an array in php - Stack Overflow
php - How to convert object as array Laravel 4.2 - Stack Overflow
php - Laravel Object to Array - Stack Overflow
php - Converting object to array in laravel - Stack Overflow
You can use below solution
$yourArray = json_decode(json_encode($yourObject), true);
and this convert object to an array for more info
Objects are, in PHP, maybe iterable. Notice, you may iterate through an object's public fields only via the foreach loop. So the following works:
$array = [];
foreach ($object as $property) {
$array[] = $property; // Stores public field only
}
var_dump($array);
Simply to get an array of object properties, you may use the get_object_vars() function.
var_dump(get_object_vars($object));
You may cast an object to be an array as @MoussabKbeisy said. And this would be the easiest way:
$array = (array) $object;
Here is another way is using ArrayIterator:
$iterator = new ArrayIterator($object);
var_dump(iterator_to_array($iterator));
You can use the toArray method.
You haven an object and you can transform it to and array.
$dataArray=$data->toArray();
You can find this method in the Laravel documentation.
If you run into problems, this will give you a pure PHP solution.
foreach($tags as $object) {
$tag_array[] = $object->name;
}
toArray is changing the collection of objects into an array of objects, like it should. If you want the objects inside the collection to also be arrays, you need to first convert each object:
$collection = DB::table('users')->where([...])->take(1)->get();
$array = $collection->map(function($obj){
return (array) $obj;
})->toArray();
To use this function in your whole app, you could add a collection macro in the boot method of one of your service providers:
Collection::macro('toArrays', function () {
return $this->map(function ($value) {
return (array) $value;
})->toArray();
});
And this at the top of the file of the service providers:
use Illuminate\Support\Collection;
Then anywhere you get a set of DB results you can just call toArrays on it:
DB::table('users')->where([...])->get()->toArrays();
Since you're getting just one object, use the first() method:
$object = DB::table('users')->where([...])->first();
$array = (array)$object;
Using array_map and casting to an array should be sufficient:
$data = array_map(function($object){
return (array) $object;
}, $data);
I also wouldn't run queries inside a loop. You should try getting that data in one query from the db. Something like this could work:
$data = DB::table('order_details')
->join('orders', 'order_details.oid', '=', 'orders.oid')
->select('order_details.oid', 'orders.ostatus')
->whereIn('order_details.oid', $oid)->get();
Edit
As it has been mentioned in another answer shortly let me explain how you can accomplish the same by setting PDO to FETCH_ASSOC:
DB::setFetchMode(PDO::FETCH_ASSOC);
$data = DB::table('order_details') .... ->get();
However this changes the fetch mode globally for the rest of the request (at least if you don't open a new connection). To be save you should change it back afterwards:
DB::setFetchMode(PDO::FETCH_ASSOC);
$data = DB::table('order_details') .... ->get();
DB::setFetchMode(PDO::FETCH_CLASS);
Or even back it up first if you can't be sure what default is used:
$fetchModeBefore = DB::getFetchMode();
DB::setFetchMode(PDO::FETCH_ASSOC);
$data = DB::table('order_details') .... ->get();
DB::setFetchMode($fetchModeBefore);
On the PDO/DB object you can set the return style to assoc.
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);
get() will return a collection. If you want to get an array of objects, use the toArray() method:
$data->toArray();
If you want to convert every object to an array too, do this:
$data->map(function($i) {
return (array)$i;
})->toArray();