dsbm49845 2016-01-16 12:57
浏览 91
已采纳

如何在php中为数组集合添加索引?

Hi I have a collection of array which I get from a foreach loop. It has no index and I want to modify it.

 $productsRange = ProductPricesInventoryTax::where('sale_price', '>=', $min_price)
                                              ->where('sale_price', '<=', $max_price)
                                              ->get();   
            foreach($productsRange as $product){
                $products = Product::where('id', '=', $product->product_id)->paginate(15);
                $productDetails = $this->prepareAllProductDetails($products);
                $array = $productDetails[0];//this returns the unidexed array
                echo "<pre>";
                print_r($array);

the array looks like this.

 Array
   (
       [id] => 1
       [sku] => 258
       [name] => Bingo Mad Angles Chaat Masti
       [is_configurable_product] => 1
       [mrp] => 20
       [sale_price] => 20
       [image] => 258-bingo-mad-angles.jpeg
       [brand] => Bingo
       [configurable_attributes] => Array
         (
             [0] => Array
                 (
                     [child_product_id] => 2
                     [name] => Weight
                     [value] => 90 gms
                     [mrp] => 20
                     [sale_price] => 20
                )

         )

  )


  Array
  (
     [id] => 3
     [sku] => 262
     [name] => India Gate Basmati Rice-Rozana
     [is_configurable_product] => 1
     [mrp] => 620
     [sale_price] => 444
     [image] => 262-india-gate.jpeg
     [brand] => India Gate
     [configurable_attributes] => Array
         (
             [0] => Array
                 ( 
                     [child_product_id] => 4
                     [name] => Weight
                     [value] => 5 Kgs
                     [mrp] => 620
                     [sale_price] => 444
                 )

         )

  )

But Now I want the array to look something like this which has array index on every array.

Array
(
    [0] => Array
        (
            [id] => 1
            [sku] => 258
            [name] => Bingo Mad Angles Chaat Masti
            [is_configurable_product] => 1
            [mrp] => 20
            [sale_price] => 20
            [image] => 258-bingo-mad-angles.jpeg
            [brand] => Bingo
            [configurable_attributes] => Array
                (
                    [0] => Array
                        (
                            [child_product_id] => 2
                            [name] => Weight
                            [value] => 90 gms
                            [mrp] => 20
                            [sale_price] => 20
                        )

                )

        )

    [1] => Array
        (
            [id] => 3
            [sku] => 262
            [name] => India Gate Basmati Rice-Rozana
            [is_configurable_product] => 1
            [mrp] => 620
            [sale_price] => 444
            [image] => 262-india-gate.jpeg
            [brand] => India Gate
            [configurable_attributes] => Array
                (
                    [0] => Array
                        (
                            [child_product_id] => 4
                            [name] => Weight
                            [value] => 5 Kgs
                            [mrp] => 620
                            [sale_price] => 444
                        )

                )

        )

Please help .

  • 写回答

2条回答 默认 最新

  • duangouyan3328 2016-01-16 13:14
    关注

    Just append $productDetails[0] to a new array and print out the result outside the foreach

    $array[] = $productDetails[0]; //this returns the unidexed array
    

    Then, outside the foreach

    print_r($array);
    

    All your code stays the same, except for

    echo "<pre>";
    print_r($array);
    

    Which are not needed anymore, since we moved the output outside the loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入