melvinfree
melvinfree

Reputation: 25

Acces nested json

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

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"}}}}"

SCRIPT details above, basically, i need to replacesome how [static product id] details in comments

$arr = [
    'inventory_id' => $inventoryid,
    'products' => []
];

$q = $dbc->query("SELECT bpID FROM Products LIMIT 3");
while($row = mysqli_fetch_assoc($q)) {
    $bpID = $row['bpID'];



$arr['products'][] = $row['bpID'];

}

$getproductidsp = [
    'method'     => 'getInventoryProductsData',
    'parameters' => json_encode($arr),
];

$ch = curl_init();
$output = curl_exec($ch);
$data = json_decode($output, true);
var_dump($data);


foreach($data['products'][$bpID]['links'] as $key => $val){

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



$query = "UPDATE IGNORE Products SET spID='$prod', svID='$variant' WHERE bpID='$bpID';";
$result = mysqli_query ($dbc, $query) or die (mysqli_error($dbc));
echo $result;
mysqli_close($dbc);

Upvotes: 1

Views: 42

Answers (1)

benkov
benkov

Reputation: 194

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'];
}

edit2: looping products

Upvotes: 2

Related Questions