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

时间:2014-01-08 08:42:53

标签: php php-5.3

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

由于上述循环,我得到以下结构。 现在,如何在category_id之后添加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
                                        )
                                )
                        )
                )
        )

我该如何处理这种情况。

我的预期输出

对于样品III,只显示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
          )

3 个答案:

答案 0 :(得分:1)

试试这个

$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;
 }

答案 1 :(得分:0)

试试这个:

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);
}

或者也许:

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;
}

答案 2 :(得分:0)

$ this-> api-> queryAllProducts返回级联对象数组,因此您应该尝试从结果中获取产品数组,然后根据您的要求构建自己的数组。

我在这里更新了您的代码,试试这个......

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));
}

}