如何在PHP中使用json解码访问多维数组中的对象?

How to access object inside multidimensional array with json decode in PHP?

提问人: 提问时间:6/17/2019 最后编辑:Community 更新时间:6/17/2019 访问量:190

问:

我正在努力访问数组的元素。我确信这很容易,但我总是遇到未定义的索引错误。

显示阵列 :

// Get response
$synresponse = curl_exec($chs);
// Decode
$variants = json_decode($synresponse, true); 

返回:

{
    "code": 200,
    "result": {
        "sync_product": {
            "id": 118425517,
            "external_id": "5d02e1420b4007",
            "name": "Women's Crop Top",
            "variants": 4,
            "synced": 4
        },
        "sync_variants": [
            {
                "id": 1335801688,
                "external_id": "5d02e1420b4141",
                "sync_product_id": 118425517,
                "name": "Women's Crop Top - S",
                "synced": true,
                "variant_id": 8039,
                "retail_price": "22.50",
                "currency": "USD",
                "product": {
                    "variant_id": 8039,
                    "product_id": 224,
                    "image": "https://d1yg28hrivmbqm.cloudfront.net/products/224/8039_1550478004.jpg",
                    "name": "Los Angeles Apparel 2332 Fine Jersey Short Sleeve Cropped T-Shirt /w Tear Away Label (White / S)"
                },
                "files": [
                    {
                        "id": 120674338,
                        "type": "default",
                        "hash": "6238a66581153315488397a5788216b2",
                        "url": null,
                        "filename": "logo2.png",
                        "mime_type": "image/png",
                        "size": 163254,
                        "width": 1380,
                        "height": 1380,
                        "dpi": null,
                        "status": "ok",
                        "created": 1560465358,
                        "thumbnail_url": "https://d1yg28hrivmbqm.cloudfront.net/files/623/6238a66581153315488397a5788216b2_thumb.png",
                        "preview_url": "https://d1yg28hrivmbqm.cloudfront.net/files/623/6238a66581153315488397a5788216b2_preview.png",
                        "visible": true
                    },
                    {
                        "id": 120686478,
                        "type": "preview",
                        "hash": "fa1a4b2f561f28529f4230ba49eca1db",
                        "url": null,
                        "filename": "mockup-79b7c377.png",
                        "mime_type": "image/png",
                        "size": 113878,
                        "width": 600,
                        "height": 600,
                        "dpi": 43,
                        "status": "ok",
                        "created": 1560469826,
                        "thumbnail_url": "https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_thumb.png",
                        "preview_url": "https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_preview.png",
                        "visible": false
                    }
                ],
                "options": []
            },
            {
                "id": 1335801689,
                "external_id": "5d02e1420b41b9",
                "sync_product_id": 118425517,
                "name": "Women's Crop Top - M",
                "synced": true,
                "variant_id": 8040,
                "retail_price": "22.50",
                "currency": "USD",
                "product": {
                    "variant_id": 8040,
                    "product_id": 224,
                    "image": "https://d1yg28hrivmbqm.cloudfront.net/products/224/8040_1550478002.jpg",
                    "name": "Los Angeles Apparel 2332 Fine Jersey Short Sleeve Cropped T-Shirt /w Tear Away Label (White / M)"
                },
                "files": [
                    {
                        "id": 120674338,
                        "type": "default",
                        "hash": "6238a66581153315488397a5788216b2",
                        "url": null,
                        "filename": "logo2.png",
                        "mime_type": "image/png",
                        "size": 163254,
                        "width": 1380,
                        "height": 1380,
                        "dpi": null,
                        "status": "ok",
                        "created": 1560465358,
                        "thumbnail_url": "https://d1yg28hrivmbqm.cloudfront.net/files/623/6238a66581153315488397a5788216b2_thumb.png",
                        "preview_url": "https://d1yg28hrivmbqm.cloudfront.net/files/623/6238a66581153315488397a5788216b2_preview.png",
                        "visible": true
                    },
                    {
                        "id": 120686478,
                        "type": "preview",
                        "hash": "fa1a4b2f561f28529f4230ba49eca1db",
                        "url": null,
                        "filename": "mockup-79b7c377.png",
                        "mime_type": "image/png",
                        "size": 113878,
                        "width": 600,
                        "height": 600,
                        "dpi": 43,
                        "status": "ok",
                        "created": 1560469826,
                        "thumbnail_url": "https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_thumb.png",
                        "preview_url": "https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_preview.png",
                        "visible": false
                    }
                ],
                "options": []
            },
            {
                "id": 1335801690,
                "external_id": "5d02e1420b41f3",
                "sync_product_id": 118425517,
                "name": "Women's Crop Top - L",
                "synced": true,
                "variant_id": 8041,
                "retail_price": "22.50",
                "currency": "USD",
                "product": {
                    "variant_id": 8041,
                    "product_id": 224,
                    "image": "https://d1yg28hrivmbqm.cloudfront.net/products/224/8041_1550478000.jpg",
                    "name": "Los Angeles Apparel 2332 Fine Jersey Short Sleeve Cropped T-Shirt /w Tear Away Label (White / L)"
                },
                "files": [
                    {
                        "id": 120674338,
                        "type": "default",
                        "hash": "6238a66581153315488397a5788216b2",
                        "url": null,
                        "filename": "logo2.png",
                        "mime_type": "image/png",
                        "size": 163254,
                        "width": 1380,
                        "height": 1380,
                        "dpi": null,
                        "status": "ok",
                        "created": 1560465358,
                        "thumbnail_url": "https://d1yg28hrivmbqm.cloudfront.net/files/623/6238a66581153315488397a5788216b2_thumb.png",
                        "preview_url": "https://d1yg28hrivmbqm.cloudfront.net/files/623/6238a66581153315488397a5788216b2_preview.png",
                        "visible": true
                    },
                    {
                        "id": 120686478,
                        "type": "preview",
                        "hash": "fa1a4b2f561f28529f4230ba49eca1db",
                        "url": null,
                        "filename": "mockup-79b7c377.png",
                        "mime_type": "image/png",
                        "size": 113878,
                        "width": 600,
                        "height": 600,
                        "dpi": 43,
                        "status": "ok",
                        "created": 1560469826,
                        "thumbnail_url": "https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_thumb.png",
                        "preview_url": "https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_preview.png",
                        "visible": false
                    }
                ],
                "options": []
            },
            {
                "id": 1335801691,
                "external_id": "5d02e1420b4238",
                "sync_product_id": 118425517,
                "name": "Women's Crop Top - XL",
                "synced": true,
                "variant_id": 8042,
                "retail_price": "22.50",
                "currency": "USD",
                "product": {
                    "variant_id": 8042,
                    "product_id": 224,
                    "image": "https://d1yg28hrivmbqm.cloudfront.net/products/224/8042_1550478006.jpg",
                    "name": "Los Angeles Apparel 2332 Fine Jersey Short Sleeve Cropped T-Shirt /w Tear Away Label (White / XL)"
                },
                "files": [
                    {
                        "id": 120674338,
                        "type": "default",
                        "hash": "6238a66581153315488397a5788216b2",
                        "url": null,
                        "filename": "logo2.png",
                        "mime_type": "image/png",
                        "size": 163254,
                        "width": 1380,
                        "height": 1380,
                        "dpi": null,
                        "status": "ok",
                        "created": 1560465358,
                        "thumbnail_url": "https://d1yg28hrivmbqm.cloudfront.net/files/623/6238a66581153315488397a5788216b2_thumb.png",
                        "preview_url": "https://d1yg28hrivmbqm.cloudfront.net/files/623/6238a66581153315488397a5788216b2_preview.png",
                        "visible": true
                    },
                    {
                        "id": 120686478,
                        "type": "preview",
                        "hash": "fa1a4b2f561f28529f4230ba49eca1db",
                        "url": null,
                        "filename": "mockup-79b7c377.png",
                        "mime_type": "image/png",
                        "size": 113878,
                        "width": 600,
                        "height": 600,
                        "dpi": 43,
                        "status": "ok",
                        "created": 1560469826,
                        "thumbnail_url": "https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_thumb.png",
                        "preview_url": "https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_preview.png",
                        "visible": false
                    }
                ],
                "options": []
            }
        ]
    },
    "extra": []
}

我正在尝试从以下位置访问元素:files

foreach($variants['result'] as $variant){

//access file images
$imgurl = $variant['files']['preview_url'];

echo '<img src="$imgurl">';
echo '<br>';

}

我收到错误:

注意:未定义的索引:文件

注意:未定义索引:preview_url

有人可以向我解释一下我不明白的地方吗?

php 数组 json undefined-index

评论

2赞 dWinder 6/17/2019
也许?foreach($variants['result']["sync_variants"] as $variant){

答:

0赞 Anton 6/17/2019 #1

此代码应该会有所帮助。问题与附加子阵列有关。

foreach($variants['result']['sync_variants'] as $variant) {
    foreach($variant['files'] as $file) {
        //access file images
        $imgurl = $file['preview_url'];

        echo '<img src="' . $imgurl . '">';
        echo '<br>';
    }
}

评论的答案:

foreach($variants['result']['sync_variants'] as $variant) {
    foreach($variant['files'] as $file) {
        if ($file['type'] != 'preview') {
            continue;
        }

        //access file images
        $imgurl = $file['preview_url'];    

        echo '<img src="' . $imgurl . '">';
        echo '<br>';
    }
}

评论

0赞 6/17/2019
是的,这就是答案。既然您提供了正确的答案,您能告诉我如何从文件而不是第一个preview_url访问第二个preview_url吗?我如何从“id”: 120686478获取网址?
0赞 6/17/2019
是的,谢谢,但您对评论的回答不是动态的。每个产品的 ID 都会发生变化。
0赞 Anton 6/17/2019
@Thisisme 我已按照之前的建议将代码更改为按类型检查
0赞 Emma 6/17/2019 #2

我的猜测是,有多个图像,不确定需要哪一个,它可能是以下任何一个:

$variant['files'][1]['preview_url']

$variant['files'][0]['preview_url']

测试

$variants = json_decode($variants, true);
$html = '';
foreach ($variants['result']["sync_variants"] as $variant) {
    $html .= '<img src="' . $variant['files'][1]['preview_url'] . '"><br>';
}

echo $html;

输出

<img src="https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_preview.png"><br>
<img src="https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_preview.png"><br>
<img src="https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_preview.png"><br>
<img src="https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_preview.png"><br>

评论

1赞 6/17/2019
谢谢你。我得到未定义的索引 1。我很想访问第二preview_url