加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 22:51:16 所属栏目: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)))); } 由
使用以下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;
 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读