【问题标题】:How to get the specific array from collection in Laravel如何从 Laravel 中的集合中获取特定数组
【发布时间】:2021-12-26 10:34:31
【问题描述】:

我的收藏中有这个

 [
  "pending" => array:2 [
           "count" => 2
           "amount" => 200
           ]
  "ongoing" => array:2 [
           "count" => 3
           "amount" => 3000
          ]
  "price_total" => 7000
]

我想要实现的是将 countamount

分开
 [
  "pending" => array:1 [
           "amount" => 200
           ]
  "ongoing" => array:1 [
           "amount" => 3000
          ]
  "discount_total" => 7000
]

我对 Laravel 和集合的概念非常陌生,我也尝试阅读文档。但它对我来说非常模糊。

【问题讨论】:

  • 这背后的代码怎么样?

标签: laravel collections


【解决方案1】:

这只会保留金额:

 $array = [
        "pending" => [
            "count" => 2,
            "amount" => 200
        ],
        "ongoing" => [
            "count" => 3,
            "amount" => 3000
        ],
        "price_total" => 7000
    ];
    foreach ($array as $key => $item) {
        if (is_array($item)) {
            if (isset($item['amount'])){
                $array[$key] = $item['amount'];
            }

        }
    }

   return $array;
 

【讨论】:

    猜你喜欢
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 2016-11-19
    • 1970-01-01
    • 2019-11-20
    • 2019-10-23
    • 2016-12-11
    相关资源
    最近更新 更多