Konwertuj obiekt laravel na tablicę

Wyjście Laravel:

Array
(
    [0] = stdClass Object
    (
        [ID] = 5

    )

    [1] = stdClass Object
    (
        [ID] = 4

    )

)

Chcę przekonwertować to na normalną tablicę. Po prostu chcę to usunąć stdClass Object. Próbowałem też użyć ->toArray(); ale dostaję błąd:

Wywołanie funkcji członkowskiej ToArray () na nie-obiekcie.

Jak mogę to naprawić?

Funkcjonalności zostały zaimplementowane na http://www.srihost.com

Author: The Alpha, 2014-10-03

10 answers

Aktualizacja od wersji 5.4 Laravel nie jest już możliwa.

Możesz zmienić konfigurację db, tak jak sugerował @Varun, lub jeśli chcesz to zrobić właśnie w tym przypadku, to:

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);
 34
Author: Jarek Tkaczyk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-10 01:57:51
foreach($yourArrayName as $object)
{
    $arrays[] = $object->toArray();
}
// Dump array with object-arrays
dd($arrays);

Lub gdy toArray() zawodzi, ponieważ jest to stdClass

foreach($yourArrayName as $object)
{
    $arrays[] =  (array) $object;
}
// Dump array with object-arrays
dd($arrays);
Nie działa? Może znajdziesz odpowiedź tutaj:

Konwertuj obiekt PHP na tablicę asocjacyjną

 20
Author: mauricehofman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:18:11

Możesz również uzyskać wszystkie wyniki zawsze jako tablicę, zmieniając

// application/config/database.php

'fetch' => PDO::FETCH_CLASS,
 // to
'fetch' => PDO::FETCH_ASSOC,
Mam nadzieję, że to pomoże.
 7
Author: Varun Varunesh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-13 05:26:41

To mi pomogło:

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

Lub

$data=array_map(function($item){
    return (array) $item;
},DB::table('table_name')->select(.......)->get());
 5
Author: Touhid,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-03 17:45:40

To działało u mnie w laravel 5.4

$partnerProfileIds = DB::table('partner_profile_extras')->get()->pluck('partner_profile_id');
$partnerProfileIdsArray = $partnerProfileIds->all();

Wyjście

array:4 [▼
  0 => "8219c678-2d3e-11e8-a4a3-648099380678"
  1 => "28459dcb-2d3f-11e8-a4a3-648099380678"
  2 => "d5190f8e-2c31-11e8-8802-648099380678"
  3 => "6d2845b6-2d3e-11e8-a4a3-648099380678"
]

Https://laravel.com/api/5.4/Illuminate/Support/Collection.html#method_all

 2
Author: scandar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-22 10:13:28

Musisz iterację nad tablicą

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

Ans użyj (array) konwersji, ponieważ masz tablicę obiektów klasy Std, a nie sam obiekt

Przykład:

$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;

Wyjście To:

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" } } 

Zgodnie z oczekiwaniami. Obiekt stdClass został przekonwertowany do tablicy.

 1
Author: Marcin Nabiałek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-03 06:52:30

Proponuję po prostu zrobić to w swojej metodzie

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

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

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

Z tym wszystkim, czego potrzebujesz, to przekazać łańcuch 'array' jako swój argument i Voila! Zwracana jest tablica asocjacyjna.

 0
Author: Dammy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-08 11:02:54

Jeśli chcesz uzyskać tylko ID w tablicy, możesz użyć array_map:

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

Z tym, zwraca tablicę z ID w każdym pos.

 0
Author: Ariel Ruiz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-15 17:42:56

To bardzo proste. Możesz użyć tak: -

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

 0
Author: kunal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-10 05:05:52

Na wypadek, gdyby ktoś nadal szukał odpowiedzi. Można to zrobić za pomocą zwykłego PHP. Łatwiejszym sposobem jest odwrócenie - JSON obiektu.

function objectToArray(&$object)
{
    return @json_decode(json_encode($object), true);
}
 0
Author: Selay,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-19 03:19:44