Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 288 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert laravel object to array

#1
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

[To see links please register here]

Reply

#2
You need to iterate over the array

for ($i = 0, $c = count($array); $i < $c; ++$i) {
$array[$i] = (array) $array[$i];
}


ans use `(array)` conversion because you have array of objects of Std class and not object itself

**Example:**

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

var_dump($users);

echo "<br /><br />";

for ($i = 0, $c = count($users); $i < $c; ++$i) {
$users[$i] = (array) $users[$i];
}
var_dump($users);
exit;

Output for this is:

array(1) { [0]=> object(stdClass)#258 (8) { ["id"]=> int(1) ["user_name"]=> string(5) "admin" ["email"]=> string(11) "admin@admin" ["passwd"]=> string(60) "$2y$10$T/0fW18gPGgz0CILTy2hguxNpcNjYZHsTyf5dvpor9lYMw/mtKYfi" ["balance"]=> string(4) "0.00" ["remember_token"]=> string(60) "moouXQOJFhtxkdl9ClEXYh9ioBSsRp28WZZbLPkJskcCr0325TyrxDK4al5H" ["created_at"]=> string(19) "2014-10-01 12:00:00" ["updated_at"]=> string(19) "2014-09-27 12:20:54" } }

array(1) { [0]=> array(8) { ["id"]=> int(1) ["user_name"]=> string(5) "admin" ["email"]=> string(11) "admin@admin" ["passwd"]=> string(60) "$2y$10$T/0fW18gPGgz0CILTy2hguxNpcNjYZHsTyf5dvpor9lYMw/mtKYfi" ["balance"]=> string(4) "0.00" ["remember_token"]=> string(60) "moouXQOJFhtxkdl9ClEXYh9ioBSsRp28WZZbLPkJskcCr0325TyrxDK4al5H" ["created_at"]=> string(19) "2014-10-01 12:00:00" ["updated_at"]=> string(19) "2014-09-27 12:20:54" } }

as expected. Object of `stdClass` has been converted to array.
Reply

#3
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:

[To see links please register here]

Reply

#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());
Reply

#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.
Reply

#6
I suggest you simply do this within your method


public function MyAwesomeMethod($returnQueryAs = null)
{
$tablename = 'YourAwesomeTable';

if($returnQueryAs == 'array')
{
DB::connection()->setFetchMode(PDO::FETCH_ASSOC);
}

return DB::table($tablename)->get();
}

With this all you need is to pass the string 'array' as your argument and Voila! An Associative array is returned.
Reply

#7
If you want to get only ID in array, can use array_map:

$data = array_map(function($object){
return $object->ID;
}, $data);

With that, return an array with ID in every pos.
Reply

#8
Its very simple. You can use like this :-

Suppose You have one users table and you want to fetch the id only
$users = DB::table('users')->select('id')->get();
$users = json_decode(json_encode($users)); //it will return you stdclass object
$users = json_decode(json_encode($users),true); //it will return you data in array
echo '<pre>'; print_r($users);
Hope it helps
Reply

#9
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);
}
Reply

#10
```
$res = ActivityServer::query()->select('channel_id')->where(['id' => $id])->first()->attributesToArray();
```

I use `get()`, it returns an object, I use the `attributesToArray()` to change the object attribute to an array.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through