Home > Backend Development > PHP Tutorial > php 数组的相同值的有关问题

php 数组的相同值的有关问题

WBOY
Release: 2016-06-13 13:31:02
Original
979 people have browsed it

php 数组的相同值的问题
有一个php 二维数组:
Array
(
  [0] => Array
  (
  [c_id] => 3
  [c_name] => 南京
  [b_key] => E_12319
  [n_key] => asdqwe8P  
  )
 
  [1] => Array
  (
  [c_name] => 普莱克斯1
  [b_key] => E_11544
  [n_key] => asdqwe8P
  )
  [2] => Array
  (
  [c_name] => 普莱克斯22
  [b_key] => E_12319
  [n_key] => asdqwe8P
  )
  [3] => Array
  (
  [c_name] => 普莱克斯33
  [b_key] => E_11544
  [n_key] => asdqwe8P
  )
  [4] => Array
  (
  [c_name] => 普莱克斯444
  [b_key] => E_12319
  [n_key] => asdqwe8P
  )

)

一个这样的数组,然后希望能将b_key相同的项合并,最终效果如下:
Array
(
  [0] => Array
  (
  [c_name] => 南京,普莱克斯22,普莱克斯444
  [b_key] => E_12319
  [n_key] => asdqwe8P  
  )
 
  [1] => Array
  (
  [c_name] => 普莱克斯1, 普莱克斯33
  [b_key] => E_11544
  [n_key] => asdqwe8P
  )
  )
)

请问应该如何实现? 基础问题 谢谢

------解决方案--------------------
就是。把你格式化还需要大把时间。下次不可以拉。

PHP code
$arr=Array
(
  '0' => Array
  (
  'c_id' => 3,
  'c_name' => '南京',
  'b_key' => 'E_12319',
  'n_key' => 'asdqwe8P'  
  ),
 
  '1' => Array
  (
  'c_name' => '普莱克斯1',
  'b_key' => 'E_11544',
  'n_key' => 'asdqwe8P'
  ),
  '2' => Array
  (
  'c_name' => '普莱克斯22',
  'b_key' => 'E_12319',
  'n_key' => 'asdqwe8P'
  ),
  '3' => Array
  (
  'c_name' => '普莱克斯33',
  'b_key' => 'E_11544',
  'n_key' => 'asdqwe8P'
  ),
  '4' => Array
  (
  'c_name' => '普莱克斯444',
  'b_key' => 'E_12319',
  'n_key' => 'asdqwe8P'
  )

);
foreach($arr as $v){
     if(!$tem[$v[b_key]])
          $tem[$v['b_key']]=$v;
     else
          $tem[$v['b_key']]['c_name'].=','.$v['c_name'];     
}
print_r(array_values($tem)); <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
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