php 多维数组添加键值,如何在多维数组php中推送一个键值对

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-29 02:36   23   0

使用以下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

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP