Home > Backend Development > PHP Tutorial > PHP sorts the two-dimensional array into a new two-dimensional array according to the key value of apname

PHP sorts the two-dimensional array into a new two-dimensional array according to the key value of apname

零到壹度
Release: 2023-03-23 14:04:02
Original
1334 people have browsed it

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;

}
Copy after login

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)));
  }
}
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template