Home > Backend Development > PHP Tutorial > PHP中二维数组替换有关问题

PHP中二维数组替换有关问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 10:43:42
Original
1089 people have browsed it

PHP中二维数组替换问题
A数组:
$dbData = array(
  array(0, '张三', '吃饭'),
  array(1, '李四', '吃饭'),
  array(2, '麻子', '吃饭'),
  array(3, '麻子', '喝水')
);
B数组
$excelData = array(
  array(0,'zhangsan', '张三'), 
  array(1,'lisi', '李四'), 
  array(2,'mazi ', '麻子')  
);
想用数组B去替换掉数组A中的元素,张三用zhangsan代替,李四用lisi代替,麻子用mazi代替
得出这样的结果:
c数组:
$dbData = array(
  array(0, 'zhangsan', '吃饭'),
  array(1, 'lisi', '吃饭'),
  array(2, 'mazi', '吃饭'),
  array(3, 'mazi', '喝水')
);

------解决方案--------------------

PHP code
$dbData = array(  array(0, '张三', '吃饭'),  array(1, '李四', '吃饭'),  array(2, '麻子', '吃饭'),  array(3, '麻子', '喝水'));$excelData = array(  array(0,'zhangsan', '张三'),    array(1,'lisi', '李四'),    array(2,'mazi ', '麻子')   );//改造 $excelData 因为是替换标的,所以相关键值对必是唯一的foreach($excelData as $v) $dict[$v[2]] = $v;//开始替换foreach($dbData as &$v) $v[1] = $dict[$v[1]][1];//检查结果print_r($dbData);<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
Latest Issues
How to create an array within an array?
From 1970-01-01 08:00:00
0
0
0
php array
From 1970-01-01 08:00:00
0
0
0
Array to array
From 1970-01-01 08:00:00
0
0
0
php array rotation
From 1970-01-01 08:00:00
0
0
0
array
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template