简体   繁体   中英

How to add index to a collection of arrays in 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 .

If you are trying to declare an array in PHP, a few things need to be added. You need commas between every subsequent item. Also, the key should not be in braces:

array(
   "id" => 1,
   "sku" => 258,
   "name" => "Bingo Mad Angles Chaat Masti",
   ...,
   "configurable attributes" => array(
                                   0 => array(
                                        "child_product_id" => 4,
                                        "name" => "Weight",
                                        ...)
                                      )
)

To make an array of arrays, you use similar syntax:

array(
    1 => array(
            "id" => 1, ... ),
    2 => array(
            "id" => 3, ... )
)

This site is particularly helpful. Hope this helps!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM