使用 Laravel 助手转换数据

我有一个具有以下格式的数组:


[

  {

    "stat_month": "01-2019",

    "in_sum": 45443,

    "out_sum": 42838,

    "balance": 2605

  },

  {...}

]

但我想将这个数组转换为:


[

  "labels" => ["01-2019", "02-2019", "03-2019"],

  "in_sum" => [45443, 60947, 56734],

  "out_sum" => [42838, 42151, 75486],

  "balance" => [2605, 18796, -18752]

]

任何想法如何通过收集帮助函数在一次操作中解决这个问题?


守着一只汪
浏览 112回答 3
3回答

拉莫斯之舞

查看 Laravel 集合中的 mapToGroups: https ://laravel.com/docs/5.8/collections#method-maptogroups或这个解决方案:$obj1 = new \stdClass;$obj1->stat_month = "01-2019";$obj1->in_sum = 45443;$obj1->out_sum = 42838;$obj1->balance = 2605;$obj2 = new \stdClass;$obj2->stat_month = "02-2019";$obj2->in_sum = 55443;$obj2->out_sum = 52838;$obj2->balance = 3605;$collection = collect([    $obj1,$obj2]);$aResult = [    'labels' => [],    'in_sum' => [],    'out_sum' => [],    'balance' => []];$collection->each(function ($item, $key) use (&$aResult) {    $aResult['labels'][] = $item->stat_month;    $aResult['in_sum'][] = $item->in_sum;    $aResult['out_sum'][] = $item->out_sum;    $aResult['balance'][] = $item->balance;});结果:array:4 [▼  "labels" => array:2 [▼    0 => "01-2019"    1 => "02-2019"  ]  "in_sum" => array:2 [▼    0 => 45443    1 => 55443  ]  "out_sum" => array:2 [▼    0 => 42838    1 => 52838  ]  "balance" => array:2 [▼    0 => 2605    1 => 3605  ]]

翻翻过去那场雪

你可以这样做是这样的php:<?php$array = '[&nbsp; {&nbsp; &nbsp; "stat_month": "01-2019",&nbsp; &nbsp; "in_sum": 45443,&nbsp; &nbsp; "out_sum": 42838,&nbsp; &nbsp; "balance": 2605&nbsp; },&nbsp; {&nbsp; &nbsp; "stat_month": "01-2019",&nbsp; &nbsp; "in_sum": 45443,&nbsp; &nbsp; "out_sum": 42838,&nbsp; &nbsp; "balance": 2605&nbsp; },&nbsp; {&nbsp; &nbsp; "stat_month": "01-2019",&nbsp; &nbsp; "in_sum": 45443,&nbsp; &nbsp; "out_sum": 42838,&nbsp; &nbsp; "balance": 2605&nbsp; }]';$array = json_decode($array, true);$arrayResult = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'stat_month' => array_column($array, 'stat_month'),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'in_sum' => array_column($array, 'in_sum'),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'out_sum' => array_column($array, 'out_sum'),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'balance' => array_column($array, 'balance')];echo "<pre>";print_r($arrayResult);?>

跃然一笑

您可以使用简化的 array_map()并接收所需的输出:$i = 0;$tmp = ['labels'=>[],'in_sum'=>[],'out_sum'=>[],'balance'=>[]];array_map(function($obj) use (&$tmp, $i){&nbsp; &nbsp; foreach($tmp as &$val){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;$val[] = array_values(((array)$obj))[$i];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;$i++;&nbsp; &nbsp; }},$ar);演示或者只是简单的foreach loop:$tmp = ['labels'=>[],'in_sum'=>[],'out_sum'=>[],'balance'=>[]];foreach($ar as $obj) {&nbsp; &nbsp; $i = 0;&nbsp; &nbsp; foreach($tmp as &$val){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;$val[] = array_values(((array)$obj))[$i];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;$i++;&nbsp; &nbsp; }}&nbsp;演示2你可以用 Laravel 的map()函数替换和重写这段代码。尝试使用动态循环而不是预定义对象的属性。
打开App,查看更多内容
随时随地看视频慕课网APP