Home > Backend Development > PHP Tutorial > 怎么合并数组相同的值

怎么合并数组相同的值

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-23 13:17:48
Original
1296 people have browsed it

求大神指导,我想把数组相同的值合并,原本是这样的
array(5) {
  [0] => array(1) {
    ["id"] => string(1) "2"
  }
  [1] => array(1) {
    ["id"] => string(1) "2"
  }
  [2] => array(1) {
    ["id"] => string(1) "1"
  }
  [3] => array(1) {
    ["id"] => string(1) "1"
  }
}
我想要的效果是以下这样的
array(5) {
  [0] => array(1) {
    ["id"] => string(1) "1"
  }
  [1] => array(1) {
    ["id"] => string(1) "2"
  }
}


回复讨论(解决方案)

$ar = array (  0 => array (    "id" => "2"  ),  1 => array (    "id" => "2"  ),  2 => array (    "id" => "1"  ),  3 => array (    "id" => "1"  ),);$arr = array();foreach($ar as $v){   if(!isset($arr[$v['id']])) $arr[$v['id']] = $v;}ksort($arr);print_r(array_values($arr));
Copy after login

Related labels:
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