如何在 PHP 中使用 json 解码访问多维数组中的对象?
How to access object inside multidimensional array with json decode in PHP?
我正在努力访问数组元素。我确信这很容易,但我总是遇到未定义的索引错误。
显示数组:
// Get response
$synresponse = curl_exec($chs);
// Decode
$variants = json_decode($synresponse, true);
Returns :
{
"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>';
}
我收到错误:
Notice: Undefined index: files
Notice: Undefined index: preview_url
有人可以向我解释一下我不明白的地方吗?
此代码应该有所帮助。问题与附加子阵列有关。
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>';
}
}
我的猜测是,有多个图像,不确定需要哪一个,可能是以下任何一个:
$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>
我正在努力访问数组元素。我确信这很容易,但我总是遇到未定义的索引错误。
显示数组:
// Get response
$synresponse = curl_exec($chs);
// Decode
$variants = json_decode($synresponse, true);
Returns :
{
"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>';
}
我收到错误:
Notice: Undefined index: files
Notice: Undefined index: preview_url
有人可以向我解释一下我不明白的地方吗?
此代码应该有所帮助。问题与附加子阵列有关。
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>';
}
}
我的猜测是,有多个图像,不确定需要哪一个,可能是以下任何一个:
$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>