Home > other >  Acces nested json
Acces nested json

Time:11-24

Please, help me a bit, i tried in any possible way to acces a nested json data, but without succes.

foreach($data['products'][19]['links'][1]['shop_4000268'][2] as $key => $val){

  $product_id = $val ['product_id'];
  $variant_id = $val ['variant_id'];

var_dump($product_id);

my array

array(2) { ["status"]=> string(7) "SUCCESS" ["products"]=> array(1) { [6605995]=> array(19) { ["is_bundle"]=> bool(false) ["ean"]=> string(13) "5949234930002" ["sku"]=> string(10) "KNK-BAN-MP" ["tax_rate"]=> int(19) ["weight"]=> int(0) ["height"]=> int(0) ["width"]=> int(0) ["length"]=> int(0) ["star"]=> int(0) ["category_id"]=> int(647123) ["manufacturer_id"]=> int(174472) ["text_fields"]=> array(1) { ["name"]=> string(29) "Bandaje Box Knockout Balcescu" } ["stock"]=> array(1) { ["bl_11206"]=> int(79) } ["prices"]=> array(1) { [10929]=> int(69) } ["locations"]=> array(1) { ["bl_11206"]=> string(0) "" } ["links"]=> array(1) { ["shop_4000268"]=> array(2) { ["product_id"]=> string(13) "6665093873699" ["variant_id"]=> string(1) "0" } } ["average_cost"]=> int(0) ["average_landed_cost"]=> int(0) ["images"]=> array(2) { [1]=> string(80) "https://cdn.shopify.com/s/files/1/0273/9034/5251/products/image.jpg?v=1657567836" [2]=> string(117) "https://cdn.shopify.com/s/files/1/0273/9034/5251/products/image_37af2a9f-83c9-4a1f-84d6-3ad0e6aa62a3.jpg?v=1657567836" } } } }

json

string(701) "{"status":"SUCCESS","products":{"6605995":{"is_bundle":false,"ean":"5949234930002","sku":"KNK-BAN-MP","tax_rate":19,"weight":0,"height":0,"width":0,"length":0,"star":0,"category_id":647123,"manufacturer_id":174472,"text_fields":{"name":"Bandaje Box Knockout Balcescu"},"stock":{"bl_11206":79},"prices":{"10929":69},"locations":{"bl_11206":""},"links":{"shop_4000268":{"product_id":"6665093873699","variant_id":"0"}},"average_cost":0,"average_landed_cost":0,"images":{"1":"https:\/\/cdn.shopify.com\/s\/files\/1\/0273\/9034\/5251\/products\/image.jpg?v=1657567836","2":"https:\/\/cdn.shopify.com\/s\/files\/1\/0273\/9034\/5251\/products\/image_37af2a9f-83c9-4a1f-84d6-3ad0e6aa62a3.jpg?v=1657567836"}}}}"

CodePudding user response:

from the provided data, it works

foreach($data['products'][6605995]['links'] as $key => $val){
    print_r($val);
}

and result is

Array
(
    [product_id] => 6665093873699
    [variant_id] => 0
)

working demo -> here

in the foreach ['links'], the key is shop and the product_id and variant_id are the values

foreach($data['products'][6605995]['links'] as $key => $val){
    $prod = $val['product_id'];
    $variant = $val['variant_id'];
}
  • Related