繁体   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