I'm struggling with accessing elements of arrays. I'm sure it's easy but I always get undefined index errors.
Display Array :
// 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": []
}
I'm trying to access elements from files
like this :
foreach($variants['result'] as $variant){
//access file images
$imgurl = $variant['files']['preview_url'];
echo '<img src="$imgurl">';
echo '<br>';
}
I get errors :
Notice: Undefined index: files
Notice: Undefined index: preview_url
Can someone explain to me what I am not understanding please?
My guess is that, there are multiple images, not sure which one would be desired, it could be any of these:
$variant['files'][1]['preview_url']
or
$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>