Php 正在尝试获取laravel负载的属性名称和id

Php 正在尝试获取laravel负载的属性名称和id,php,json,laravel,api,Php,Json,Laravel,Api,我在下面有这个JSON负载,希望从负载中获取名称和ID。但是,l不能从有效负载中对象名称和ID 解码JSON $result = json_decode($resp->getBody()->getContents(), true); return response()->json( [ "code" => 200,

我在下面有这个JSON负载,希望从负载中获取名称和ID。但是,l不能从有效负载中对象名称和ID

解码JSON

 $result = json_decode($resp->getBody()->getContents(), true);
                return response()->json(
                    [
    
                        "code" => 200,
                        "message" => "OK",
                        "payload"=>  $result['payload'] ?? '',
                    ]);
Api json有效载荷

{
    "code": 200,
    "message": "OK",
    "payload": {
        "items": [
            {
                "name": "Hostel",
                "image": {
                    "name": "WEWEBBFC791FD50E347BD.jpeg"
                },
                "rate": {
                    "amount": "3.0000",
                    "currency": {
                        "code": "US"
                    }
                },
                "pricing": [
                    {
                        "rate": "3.0000"
                    }
                ],
                "id": 12, // get this id
                "created_at": "2021-02-28T11:08:25+00:00"
            }
            ..........
        ],
        "total": 10,
        "offset": 10
    }
}
用于将json解码为关联数组,然后像访问任何其他assoc数组一样访问该数组的元素

看起来您的集合中可以有一个或多个
,因此您需要使用循环对它们进行迭代

$decoded = json_decode($json, true);

foreach ($decoded['payload']['items'] as $item) {
    $name = $items['name'];
    $id = $items['id'];

    dump($name, $id);
}