【问题标题】:How to parse a laravel collection如何解析 laravel 集合
【发布时间】:2017-10-24 12:46:38
【问题描述】:

我在输出上有一个 laravel 集合,我想解析它 ->toArray()

Collection {#335
  #items: array:2 [
    "0f39b1e0-a507-11e7-9d6e-33e84951047e" => array:2 [
      "total_amount" => 25000
      "debt_type" => array:2 [
        0 => "car_loan"
        1 => "car_loan"
      ]
    ]
    "0f218520-a507-11e7-b0ba-8554a4ad039b" => array:2 [
      "total_amount" => 15000
      "debt_type" => array:1 [
        0 => "house_loan"
      ]
    ]
  ]
}

有什么方法可以解析它,所以我得到以下输出:

    array:1[
      0=>[
       'debt_id'=>'0f39b1e0-a507-11e7-9d6e-33e84951047e',
       'debt_type'=>'car_loan',
       'total_amount'=>25000
      ],
      1=>[
       'debt_id'=>'0f218520-a507-11e7-b0ba-8554a4ad039b',
       'debt_type'=>'house_loan',
       'total_amount'=>15000
      ]
]

我尝试过的方法有效,但不确定它是否是解决它的好方法:

$appDebts = $appDebts->groupBy('debt_type_id')->map(function ($item) {
    return [

        'total_amount' => $item->sum('amount'),
        'debt_type'    => $item->map(function ($item) {
            return $item->debt_type->slug;
        })->toArray(),
    ];
})->toArray();

如果你 dd $appDebts 你会得到我在帖子顶部添加的集合

$carLoan     = [];
$studentLoan = [];
$houseLoan   = [];
$cardLoan    = [];

foreach ($appDebts as $debt) {

    if ($debt['debt_type'][0] === 'car_loan') {
        $carLoan['TotalAmount'] = $debt['total_amount'];
        $carLoan['LoanType']    = $debt['debt_type'][0];

    }
    if ($debt['debt_type'][0] === 'house_loan') {

        $houseLoan['TotalAmount'] = $debt['total_amount'];
        $houseLoan['LoanType']    = $debt['debt_type'][0];
    }
    if ($debt['debt_type'][0] === 'student_loan') {

        $studentLoan['TotalAmount'] = $debt['total_amount'];
        $studentLoan['LoanType']    = $debt['debt_type'][0];
    }
    if ($debt['debt_type'][0] === 'credit_card_loan') {

        $cardLoan['TotalAmount'] = $debt['total_amount'];
        $cardLoan['LoanType']    = $debt['debt_type'][0];
    }

}

【问题讨论】:

  • 看看toArray()
  • 遍历集合并创建自己的数组,因为您想要的结构与集合的结构不匹配。
  • @JigarShah 我已经知道这个功能,这就是我在问题中提到的原因,但问题是如何?
  • @aynber 我已经这样做了,但是我不太喜欢代码的外观,我想也许有一种方法可以使用 laravel 助手来处理集合。我的方式是我将所有内容都转换为数组,然后 foreach 并构建我自己的数组。
  • @trace_le 我已经这样做了...向我们展示你的代码。

标签: php laravel collections


【解决方案1】:

基于您共享的数组:

$parsed = $collection->map(function ($item, $id) {
    return [
        'debt_id' => $id,
        'debt_type' => collect($item['debt_type'])->first(),
        'total_amount' => $item['total_amount']
    ];
})->values()->toArray();

使用values 删除key => value,您将获得没有键的数组

【讨论】:

    【解决方案2】:

    在你做的第一个映射之后尝试这个映射:

    $appDebts = $appDebts->groupBy('debt_type_id')->map(function ($item) {
        return [
    
            'total_amount' => $item->sum('amount'),
            'debt_type'    => $item->map(function ($item) {
                return $item->debt_type->slug;
            })->toArray(),
        ];
    }); // <-- remove ->toArray() from here 
    
    $appDebts = $appDebts->map(function ($item, $key) {
        return [
            'debt_type_id' => $key
            'debt_type'    => $item["debt_type"][0], // assuming you want the first type !!
            'total_amount' => $item["total_amount"],
        ];
    })->toArray();
    

    PS:这会将给定的集合转换为想要的数组以进行更多性能调整,请考虑编辑 SQL 查询或获取appDebts的逻辑

    【讨论】:

      【解决方案3】:

      我唯一可以添加到@Llopele 的答案是使用keyBy() 来更轻松地访问数据:

      $parsed = $collection->map(function ($item, $id) {
          return [
              'debt_id' => $id,
              'debt_type' => collect($item['debt_type'])->first(),
              'total_amount' => $item['total_amount']
          ];
      })->values()->keyBy('debt_type')->toArray();
      

      所以现在你可以像Arr::get($parsed, 'house_loan');这样访问数据了

      【讨论】:

        猜你喜欢
        • 2019-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-06
        • 1970-01-01
        相关资源
        最近更新 更多