将二维数组按键值相同的重组数组
时间:2021-07-01 10:21:17
帮助过:19人阅读
Array( [0] => Array ( [title] => 111 [name] => 上海 ) [1] => Array ( [title] => 111 [name] => 江苏 ) [2] => Array ( [title] => dd [name] => 上海 ) [3] => Array ( [title] => dd [name] => 江苏 ) [4] => Array ( [title] => dd [name] => 浙江 ) )按照title一样的,重新组合数组为:Array( [0] => Array ( [title] => 111 [name] => 上海,杭州 ) [1] => Array ( [title] => dd [name] => 上海,宁波,无锡 ) )
实现代码:
function array_group_concat($ar, $field, $group_by) {
$res = array();
foreach($ar as $row) {
$g = $row[$group_by];
if(isset($res[$g])) $res[$g][$f