如何解析laravel集合

时间:2017-10-24 12:46:38

标签: php laravel collections

我在输出上有一个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();

如果您使用$ 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];
    }

}

3 个答案:

答案 0 :(得分:2)

根据您分享的阵列:

$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,即可获得不带键的数组

答案 1 :(得分:0)

在您完成的第一个映射之后尝试使用此映射:

$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的逻辑

答案 2 :(得分:0)

我唯一可以添加到@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');

这样的数据了