Reputation: 4013
Using following foreach loop
$category_ids = array_of_ids;
foreach($category_ids as $category_id) {
$queryAllProducts['products'][] = $this->api->queryAllProducts(array('params' => array('categoryCode' => $category_id, 'usertoken' => USER_TOKEN))));
}
As a result of the above loop I get the following struction.
Now how can I add a category_id
after productName
.
Array
(
[0] => stdClass Object
(
[result] => stdClass Object
(
[products] => stdClass Object
(
[Product] => Array
(
[0] => stdClass Object
(
[price] => 0.72
[productName] => product_name_appears_here
)
[1] => stdClass Object
(
[price] => 0.72
[productName] => product_name_appears_here
)
[2] => stdClass Object
(
[price] => 0.72
[productName] => product_name_appears_here
)
)
)
)
)
[1] => stdClass Object
(
[result] => stdClass Object
(
[products] => stdClass Object
(
[Product] => Array
(
[0] => stdClass Object
(
[price] => 3.19
[productName] => product_name_appears_here
)
[1] => stdClass Object
(
[price] => 1.12
[productName] => product_name_appears_here
)
[2] => stdClass Object
(
[price] => 1.66
[productName] => product_name_appears_here
)
[3] => stdClass Object
(
[price] => 1.66
[productName] => product_name_appears_here
)
)
)
)
)
How can I handle this kind of situation.
My expected output
For sample Ill show just 2 array
[0] => stdClass Object
(
[price] => 0.72
[productName] => product_name_appears_here
[category_id] => something_which_is_from_$category_id
)
[1] => stdClass Object
(
[price] => 0.72
[productName] => product_name_appears_here
[category_id] => something_which_is_from_$category_id
)
Upvotes: 4
Views: 602
Reputation: 1481
Try like this
$category_ids = array_of_ids;
foreach($category_ids as $category_id) {
$rowResult = $this->api->queryAllProducts(array('params' => array('categoryCode' => $category_id, 'usertoken' => USER_TOKEN))));
foreach($rowResult->result->products->Product as $values) {
$values->category_id = {HERE CODE TO GET category_id}
}
$queryAllProducts['products'][] = $rowResult;
}
Upvotes: 1
Reputation: 232
$this->api->queryAllProducts return cascading object array, so you should try to get product array from result then build your own array as per your requirement.
Here I have updated your code, try this.....
foreach($category_ids as $category_id) {
$rsobj = $this->api->queryAllProducts(array('params' => array('categoryCode' => $category_id, 'usertoken' => USER_TOKEN))));
$Products = $rsobj->result->products->Product;
foreach($Products as $product) {
$queryAllProducts['products'][] = (object)array_merge($product, array('category_id' => something_which_is_from_$category_id));
}
}
Upvotes: 0
Reputation: 1420
try this:
foreach($category_ids as $category_id) {
$queryAllProducts['products'][] = $this->api->queryAllProducts(array('params' => array('categoryCode' => $category_id, 'usertoken' => USER_TOKEN))));
$a = array_pop($queryAllProducts);
$b = array_pop($a);
$c = array_pop($b);
$d = array_merge($c, array( 'category_id' => $category_id ));
array_push($queryAllProducts,$d);
}
or maybe:
foreach($category_ids as $category_id) {
$result = $this->api->queryAllProducts(array('params' => array('categoryCode' => $category_id, 'usertoken' => USER_TOKEN))));
$a = array_pop($result);
$b = array_merge($a, array( 'category_id' => $category_id ));
$queryAllProducts['products'][] = $b;
}
Upvotes: 0