我无法将对象转换为数组

时间:2019-10-13 16:51:39

标签: arrays laravel object shopping-cart

我正在尝试建立购物车API路线,因此我陷入了这个问题,因为当我使用此语句es.index(index = 'export_control', doc_type="_doc", body = body) 时,我得到了这样的对象:

$carts = \Cart::session($session)->getContent();

,我想让它成为对象数组。我试了"cart": { "1": { "id": 1, "name": "Tort cu biscuiți", "price": 20, "quantity": "1", "attributes": { "image_path": "http://127.0.0.1:8000/storage/images/AkiGKJdxjKNtyoVI034RPL1drLsMntUxLfzqZplV.jpeg" }, "conditions": [] }, "2": { "id": 2, "name": "Tort cu biscuiți", "price": 20, "quantity": 2, "attributes": { "image_path": "http://127.0.0.1:8000/storage/images/IkAhenLttHWaRD58hNZ460ykWCq7q1sih3vI9H5V.jpeg" }, "conditions": [] } } ,但没有用,我还整理了(数组)$ cart,我得到了:

$cart->toArray();

我能知道是什么问题吗?

1 个答案:

答案 0 :(得分:1)

您可以像这样手动操作:

$carts = \Cart::session($session)->getContent();

if ($carts) {
    $cartsArray = json_decode($carts);
    foreach($cartsArray->cart as &$cart){
        $cart = (array) $cart;
    }
}

dd($cartsArray);