$oldArr = [ ['a', 1], ['', 2], ['', 3], ['b', 4], ['', 5], ['c',6], ['',7] ]; 转换为 $newArr = [ 'a'=>'1,2,3', 'b'=>'4,5', 'c'=>'6,7' ]
<?php $oldArr = [ ['a', 1], ['', 2], ['', 3], ['b', 4], ['', 5], ['c',6], ['',7] ]; $newArray = []; $temp = ''; foreach ($oldArr as $item) { if ($item[0]) { $temp = $item[0]; $newArray[$temp] = $item[1]; continue; } $newArray[$temp] .= ','.$item[1]; } var_dump($newArray);
Shouldn’t we think about why the arrays are spliced like this?
I feel that when fetching it from the database, it can be processed with mysql built-in functionssuch as concat() or concat_group()
Shouldn’t we think about why the arrays are spliced like this?
I feel that when fetching it from the database, it can be processed with mysql built-in functions
such as concat() or concat_group()