我无法将对象转换为数组

I can't convert object to an array

我正在尝试制作购物车 API 路线,但我遇到了这个问题,因为当我使用此语句时 $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->toArray(); 但没有用,我也尝试了 (array) $cart 并且我得到了这个:

"cart": {
    "\u0000*\u0000items": {
        "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": []
        }
    }
}

请问是什么问题?

您可以像这样手动操作:

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

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

dd($cartsArray);