How to add index to a collection of arrays in php? -


hi have collection of array foreach loop. has no index , want modify it.

 $productsrange = productpricesinventorytax::where('sale_price', '>=', $min_price)                                               ->where('sale_price', '<=', $max_price)                                               ->get();                foreach($productsrange $product){                 $products = product::where('id', '=', $product->product_id)->paginate(15);                 $productdetails = $this->prepareallproductdetails($products);                 $array = $productdetails[0];//this returns unidexed array                 echo "<pre>";                 print_r($array); 

the array looks 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 want array 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 .

just append $productdetails[0] new array , print out result outside foreach

$array[] = $productdetails[0]; //this returns unidexed array 

then, outside foreach

print_r($array); 

all code stays same, except

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

which not needed anymore, since moved output outside loop.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -