1

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);
asked Nov 23, 2022 at 14:03
4
  • Did you have json_decode the data? Commented Nov 23, 2022 at 14:15
  • It looks like you are trying to access the element without using the key eg; $data['products'][19] should be $data['products'][6605995] Commented Nov 23, 2022 at 14:16
  • Sorry, wasn't explicit enough , i want product_id & variant_id Commented Nov 23, 2022 at 14:16
  • @MartijnICU basically 6605995 it's the parameter which i'm using in search and that means that i should use this variable here too to be able to build correct Commented Nov 23, 2022 at 14:20

1 Answer 1

2

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

answered Nov 23, 2022 at 14:35
Sign up to request clarification or add additional context in comments.

4 Comments

Ok, it's working but if u are nice now i have a new problem with this situation. $data['products'][6605995]['links'] how i can define here instead of [6605995] some info from json cause i will get 1k result per call, not just one to be able to use that specific id...
again need only the product_id and the variant_id ? @melvinfree
Yas, but basically, my script it's like while($row = mysqli_fetch_assoc($q)) { $bpID = $row['bpID']; ... i put those 1k rows from db in apicall , api call return me 1k results and for each to be able to use foreach i should use foreach(data[products][productid] where this product id it's = $bpID, i added in first post all of the script
i have edited my answer, it loops for products, and then loops the product and variant. @melvinfree

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.