Home >Backend Development >PHP Tutorial >PHP sorts the two-dimensional array into a new two-dimensional array according to the key value of apname
The content of this article is to share with you that PHP reorganizes the two-dimensional array into a new two-dimensional array according to the key value of apname. It has certain reference value. Friends in need can refer to it
After the construction, the display effect is as follows
Used two methods to build
Method 1,
function array_chaifen($countnum,$name){ $lists = array_column($countnum, $name); $lists = array_flip(array_flip($lists)); foreach ($lists as $k1=>$v1){ $m = []; foreach ($countnum as $k2=>$v2){ if($v2[$name] === $v1){ $m = array_merge($m,$v2); } } $lists[$k1] = $m; } return $lists; }
Method 2,
$result = []; foreach ($countnum as $key => $value) { $name = $value['apname']; $isLoad = false; $loadKey = false; foreach ($result as $k => $v) { if($v['apname'] == $value['apname']){ $isLoad = true; $loadKey = $k; } } @$keyTemp = end(array_keys($value)); if ($isLoad) { $result[$k][$keyTemp] = end($value); }else{ array_push($result, array('apname'=>$value['apname'], $keyTemp=>end($value))); } }
The above is the detailed content of PHP sorts the two-dimensional array into a new two-dimensional array according to the key value of apname. For more information, please follow other related articles on the PHP Chinese website!