如何在多维数组php中推送一个键值对
作者:互联网
使用以下foreach循环
$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))));
}
由于上述循环,我得到以下结构.
现在我如何在productName之后添加category_id.
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
)
)
)
)
)
我怎么能处理这种情况.
我的预期产量
对于样品Ill,只显示2个阵列
[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
)
解决方法:
试试这样吧
$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;
}
标签:php,php-5-3 来源: https://codeday.me/bug/20190708/1406643.html