Я не могу найти, почему мой цикл foreach застревает в первой записи, я думаю. Он возвращает только один объект массива N раз. Я проверил ответ и у него в массиве больше 1 объекта. Я не уверен, что я делаю неправильно. Вот код:foreach looping только для первого объекта
if (!empty($response) || !(isset($response[0]->errors))) {
$responseproduct = new StdClass;
$variant = new StdClass;
$responseproducts = array();
foreach ($response->products as $product) {
$variants = array();
$responseproduct->id = $product->id;
$responseproduct->name = $product->title;
$responseproduct->urlimg = $product->images[0]->src;
foreach ($product->variants as $variantobj) {
$variant->variantid = $variantobj->id;
$variant->price = $variantobj->price;
$variant->title = $variantobj->option1;
$variant->urlcheckout = "http://bebe2go.myshopify.com/cart/".$variant->variantid.":1?ref=kindeu-ads";
array_push($variants, $variant);
}
$responseproduct->variants = $variants;
array_push($responseproducts, $responseproduct);
}
$jsonResponse = array(
'success' => 'Llamada exitosa.',
'producto' => $responseproducts
);
// Replace the response body with the json encoded data
return json_encode($jsonResponse);
}
Я пропустил что-то? Спасибо за помощь заранее: D
Вот что она печатает:
{
"success": "Llamada exitosa.",
"producto": [
{
"id": 97477842,
"name": "Enfapro Premium Etapa 2",
"urlimg": "https://cdn.shopify.com/s/files/1/0154/0015/products/formula_enfapro_premium_e3.jpg?v=1343083756",
"variants": [
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
},
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
},
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
}
]
},
{
"id": 97477842,
"name": "Enfapro Premium Etapa 2",
"urlimg": "https://cdn.shopify.com/s/files/1/0154/0015/products/formula_enfapro_premium_e3.jpg?v=1343083756",
"variants": [
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
},
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
},
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
}
]
},
{
"id": 97477842,
"name": "Enfapro Premium Etapa 2",
"urlimg": "https://cdn.shopify.com/s/files/1/0154/0015/products/formula_enfapro_premium_e3.jpg?v=1343083756",
"variants": [
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
},
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
},
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
}
]
},
{
"id": 97477842,
"name": "Enfapro Premium Etapa 2",
"urlimg": "https://cdn.shopify.com/s/files/1/0154/0015/products/formula_enfapro_premium_e3.jpg?v=1343083756",
"variants": [
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
},
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
},
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
}
]
},
{
"id": 97477842,
"name": "Enfapro Premium Etapa 2",
"urlimg": "https://cdn.shopify.com/s/files/1/0154/0015/products/formula_enfapro_premium_e3.jpg?v=1343083756",
"variants": [
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
},
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
},
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
}
]
},
{
"id": 97477842,
"name": "Enfapro Premium Etapa 2",
"urlimg": "https://cdn.shopify.com/s/files/1/0154/0015/products/formula_enfapro_premium_e3.jpg?v=1343083756",
"variants": [
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
},
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
},
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
}
]
},
{
"id": 97477842,
"name": "Enfapro Premium Etapa 2",
"urlimg": "https://cdn.shopify.com/s/files/1/0154/0015/products/formula_enfapro_premium_e3.jpg?v=1343083756",
"variants": [
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
},
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
},
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
}
]
},
{
"id": 97477842,
"name": "Enfapro Premium Etapa 2",
"urlimg": "https://cdn.shopify.com/s/files/1/0154/0015/products/formula_enfapro_premium_e3.jpg?v=1343083756",
"variants": [
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
},
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
},
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
}
]
},
{
"id": 97477842,
"name": "Enfapro Premium Etapa 2",
"urlimg": "https://cdn.shopify.com/s/files/1/0154/0015/products/formula_enfapro_premium_e3.jpg?v=1343083756",
"variants": [
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
},
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
},
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
}
]
},
{
"id": 97477842,
"name": "Enfapro Premium Etapa 2",
"urlimg": "https://cdn.shopify.com/s/files/1/0154/0015/products/formula_enfapro_premium_e3.jpg?v=1343083756",
"variants": [
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
},
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
},
{
"variantid": 322904189,
"price": "445.00",
"title": "1.2kg.",
"urlcheckout": "http://bebe2go.myshopify.com/cart/322904189:1?ref=kindeu-ads"
}
]
}
]
}
А вот $ Ответ:
{
"success": "Llamada exitosa.",
"producto": {
"products": [
{
"id": 97477928,
"title": "Enfagrow Premium Etapa 3",
"variants": [
{
"id": 259621536,
"product_id": 97477928,
"title": "400gr.",
"price": "101.00",
"sku": "737763",
"position": 1,
"grams": 400,
"inventory_policy": "deny",
"compare_at_price": null,
"fulfillment_service": "manual",
"inventory_management": "shopify",
"option1": "400gr.",
"option2": null,
"option3": null,
"created_at": "2012-12-03T13:06:59-06:00",
"updated_at": "2015-09-04T10:54:34-05:00",
"requires_shipping": true,
"taxable": false,
"barcode": null,
"inventory_quantity": 101,
"old_inventory_quantity": 101,
"image_id": 193648526,
"weight": 0.4,
"weight_unit": "kg"
},
{
"id": 228143764,
"product_id": 97477928,
"title": "800gr.",
"price": "216.00",
"sku": "737771",
"position": 2,
"grams": 900,
"inventory_policy": "deny",
"compare_at_price": null,
"fulfillment_service": "manual",
"inventory_management": "shopify",
"option1": "800gr.",
"option2": null,
"option3": null,
"created_at": "2012-07-23T17:35:47-05:00",
"updated_at": "2015-09-04T10:54:34-05:00",
"requires_shipping": true,
"taxable": false,
"barcode": null,
"inventory_quantity": 89,
"old_inventory_quantity": 89,
"image_id": 193648526,
"weight": 0.9,
"weight_unit": "kg"
},
{
"id": 322904443,
"product_id": 97477928,
"title": "1.2kg.",
"price": "318.00",
"sku": "788112",
"position": 3,
"grams": 1200,
"inventory_policy": "deny",
"compare_at_price": null,
"fulfillment_service": "manual",
"inventory_management": "shopify",
"option1": "1.2kg.",
"option2": null,
"option3": null,
"created_at": "2013-06-10T10:21:57-05:00",
"updated_at": "2015-09-04T10:54:35-05:00",
"requires_shipping": true,
"taxable": false,
"barcode": null,
"inventory_quantity": 90,
"old_inventory_quantity": 90,
"image_id": 193648526,
"weight": 1.2,
"weight_unit": "kg"
}
],
"images": [
{
"id": 193648526,
"product_id": 97477928,
"position": 1,
"created_at": "2012-07-23T17:51:24-05:00",
"updated_at": "2012-07-23T17:51:24-05:00",
"src": "https://cdn.shopify.com/s/files/1/0154/0015/products/formula-enfagrow-premium-3_900_grs.jpg?v=1343083884",
"variant_ids": [
228143764,
259621536,
322904443
]
}
]
},
{
"id": 215105273,
"title": "Enfagrow Premium Etapa 3 - Sabor Vainilla",
"variants": [
{
"id": 485161409,
"product_id": 215105273,
"title": "800gr.",
"price": "235.00",
"sku": "737771",
"position": 1,
"grams": 900,
"inventory_policy": "deny",
"compare_at_price": null,
"fulfillment_service": "manual",
"inventory_management": "shopify",
"option1": "800gr.",
"option2": null,
"option3": null,
"created_at": "2014-01-15T18:03:29-06:00",
"updated_at": "2015-09-04T10:54:35-05:00",
"requires_shipping": true,
"taxable": false,
"barcode": null,
"inventory_quantity": 106,
"old_inventory_quantity": 106,
"image_id": 440133777,
"weight": 0.9,
"weight_unit": "kg"
},
{
"id": 485161417,
"product_id": 215105273,
"title": "400gr.",
"price": "115.00",
"sku": "737763",
"position": 2,
"grams": 400,
"inventory_policy": "deny",
"compare_at_price": null,
"fulfillment_service": "manual",
"inventory_management": "shopify",
"option1": "400gr.",
"option2": null,
"option3": null,
"created_at": "2014-01-15T18:03:29-06:00",
"updated_at": "2015-09-04T10:54:36-05:00",
"requires_shipping": true,
"taxable": false,
"barcode": null,
"inventory_quantity": 102,
"old_inventory_quantity": 102,
"image_id": 440133777,
"weight": 0.4,
"weight_unit": "kg"
},
{
"id": 485161425,
"product_id": 215105273,
"title": "1.2kg.",
"price": "335.00",
"sku": "788112",
"position": 3,
"grams": 1200,
"inventory_policy": "deny",
"compare_at_price": null,
"fulfillment_service": "manual",
"inventory_management": "shopify",
"option1": "1.2kg.",
"option2": null,
"option3": null,
"created_at": "2014-01-15T18:03:29-06:00",
"updated_at": "2015-09-04T10:54:37-05:00",
"requires_shipping": true,
"taxable": false,
"barcode": null,
"inventory_quantity": 101,
"old_inventory_quantity": 101,
"image_id": 440133777,
"weight": 1.2,
"weight_unit": "kg"
}
],
"images": [
{
"id": 440133777,
"product_id": 215105273,
"position": 1,
"created_at": "2014-01-15T18:03:30-06:00",
"updated_at": "2015-02-05T15:57:53-06:00",
"src": "https://cdn.shopify.com/s/files/1/0154/0015/products/formula-enfagrow-premium-3_900_grs_b9e50ef2-354e-452c-9727-13d192460e64.jpg?v=1423173473",
"variant_ids": [
485161409,
485161417,
485161425
]
}
]
},
{
"id": 136214925,
"title": "Enfamil A.R. Premium",
"variants": [
{
"id": 311269687,
"product_id": 136214925,
"title": "900gr.",
"price": "434.00",
"sku": "569348",
"position": 1,
"grams": 900,
"inventory_policy": "deny",
"compare_at_price": null,
"fulfillment_service": "manual",
"inventory_management": "shopify",
"option1": "900gr.",
"option2": null,
"option3": null,
"created_at": "2013-05-16T10:02:53-05:00",
"updated_at": "2015-09-04T10:54:37-05:00",
"requires_shipping": true,
"taxable": false,
"barcode": null,
"inventory_quantity": 107,
"old_inventory_quantity": 107,
"image_id": 265850921,
"weight": 0.9,
"weight_unit": "kg"
},
{
"id": 311269689,
"product_id": 136214925,
"title": "400gr.",
"price": "203.00",
"sku": "569321",
"position": 2,
"grams": 400,
"inventory_policy": "deny",
"compare_at_price": null,
"fulfillment_service": "manual",
"inventory_management": "shopify",
"option1": "400gr.",
"option2": null,
"option3": null,
"created_at": "2013-05-16T10:02:53-05:00",
"updated_at": "2015-09-04T10:54:38-05:00",
"requires_shipping": true,
"taxable": false,
"barcode": null,
"inventory_quantity": 110,
"old_inventory_quantity": 110,
"image_id": 265850921,
"weight": 0.4,
"weight_unit": "kg"
}
],
"images": [
{
"id": 265850921,
"product_id": 136214925,
"position": 1,
"created_at": "2013-05-16T10:05:48-05:00",
"updated_at": "2013-05-16T10:05:48-05:00",
"src": "https://cdn.shopify.com/s/files/1/0154/0015/products/formula_enfamil_premium_a.r.jpg?v=1368716748",
"variant_ids": [
311269687,
311269689
]
}
]
},
{
"id": 291704225,
"title": "Enfamil Nutramigen Premium con LGG",
"variants": [
{
"id": 688773553,
"product_id": 291704225,
"title": "357gr",
"price": "290.00",
"sku": "804847",
"position": 1,
"grams": 400,
"inventory_policy": "deny",
"compare_at_price": null,
"fulfillment_service": "manual",
"inventory_management": null,
"option1": "357gr",
"option2": null,
"option3": null,
"created_at": "2014-04-15T17:16:25-05:00",
"updated_at": "2015-09-07T16:57:07-05:00",
"requires_shipping": true,
"taxable": false,
"barcode": "",
"inventory_quantity": -4,
"old_inventory_quantity": -4,
"image_id": 717861481,
"weight": 0.4,
"weight_unit": "kg"
}
],
"images": [
{
"id": 717861481,
"product_id": 291704225,
"position": 1,
"created_at": "2014-04-16T11:18:15-05:00",
"updated_at": "2014-04-16T11:18:15-05:00",
"src": "https://cdn.shopify.com/s/files/1/0154/0015/products/feature_nutramigen_premium_llg.png?v=1397665095",
"variant_ids": [
688773553
]
}
]
},
{
"id": 136215519,
"title": "Enfamil Premium Confort 400gr.",
"variants": [
{
"id": 311272317,
"product_id": 136215519,
"title": "400gr.",
"price": "215.00",
"sku": "903124",
"position": 1,
"grams": 400,
"inventory_policy": "deny",
"compare_at_price": null,
"fulfillment_service": "manual",
"inventory_management": "shopify",
"option1": "400gr.",
"option2": null,
"option3": null,
"created_at": "2013-05-16T10:15:51-05:00",
"updated_at": "2015-09-04T10:54:39-05:00",
"requires_shipping": true,
"taxable": false,
"barcode": null,
"inventory_quantity": 100,
"old_inventory_quantity": 100,
"image_id": 265861561,
"weight": 0.4,
"weight_unit": "kg"
}
],
"images": [
{
"id": 265861561,
"product_id": 136215519,
"position": 1,
"created_at": "2013-05-16T10:37:59-05:00",
"updated_at": "2013-05-16T10:37:59-05:00",
"src": "https://cdn.shopify.com/s/files/1/0154/0015/products/Enfamil_Confort.jpg?v=1368718679",
"variant_ids": [
311272317
]
}
]
},
{
"id": 136220823,
"title": "Enfamil Premium EnfaCare",
"variants": [
{
"id": 311286757,
"product_id": 136220823,
"title": "363 gr.",
"price": "180.00",
"sku": "949760",
"position": 1,
"grams": 400,
"inventory_policy": "deny",
"compare_at_price": null,
"fulfillment_service": "manual",
"inventory_management": "shopify",
"option1": "363 gr.",
"option2": null,
"option3": null,
"created_at": "2013-05-16T10:46:08-05:00",
"updated_at": "2015-09-04T10:54:39-05:00",
"requires_shipping": true,
"taxable": false,
"barcode": null,
"inventory_quantity": 83,
"old_inventory_quantity": 83,
"image_id": 265872599,
"weight": 0.4,
"weight_unit": "kg"
}
],
"images": [
{
"id": 265872599,
"product_id": 136220823,
"position": 1,
"created_at": "2013-05-16T10:57:47-05:00",
"updated_at": "2013-05-16T10:57:47-05:00",
"src": "https://cdn.shopify.com/s/files/1/0154/0015/products/enfamil-enfacare-premium.jpg?v=1368719867",
"variant_ids": [
311286757
]
}
]
}
Можете ли вы 'print_r ($ variants)'? – aldrin27
А также '$ response' – aldrin27
Ответ вырезается наполовину из-за слишком большого количества символов. D: Редактирование: Но количество раз, когда оно печатается, такое же, как количество продуктов, на которые отвечает ответ –