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);
1 Answer 1
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
Sign up to request clarification or add additional context in comments.
4 Comments
melvinfree
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...
benkov
again need only the product_id and the variant_id ? @melvinfree
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
benkov
i have edited my answer, it loops for products, and then loops the product and variant. @melvinfree
lang-php
$data['products'][19]should be$data['products'][6605995]