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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀