簡體   English   中英

如何在php中將索引添加到數組的集合中?

[英]How to add index to a collection of arrays in php?

嗨,我有一個數組集合,我從一個foreach循環中獲取。 它沒有索引,我想修改它。

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

數組看起來像這樣。

 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
                 )

         )

  )

但是現在我希望數組看起來像這樣,每個數組上都有數組索引。

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
                        )

                )

        )

請幫忙 。

如果要在PHP中聲明數組,則需要添加一些內容。 您需要在每個后續項目之間添加逗號。 另外,密鑰不應放在大括號中:

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

要創建一個數組數組,請使用類似的語法:

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

該站點特別有用。 希望這可以幫助!

只需將$productDetails[0]附加到新數組,然后在foreach外部打印出結果

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

然后,在foreach之外

print_r($array);

您的所有代碼保持不變,除了

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

因為我們將輸出移到了循環之外,所以不再需要了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM