当前位置:网站首页>多个一维数组拆分合并为二维数组
多个一维数组拆分合并为二维数组
2022-04-23 05:39:00 【fusheng-fate】
记录一下工作中遇到的数据拆分问题。
场景一:
一维数组的结构为[value,value … ,value];
$arr1 = array('张三','李四','王五');
$arr2 = array(18,19,20);
$arr3 = array('man','man','women');
<?php
$arr1 = array('张三','李四','王五');
$arr2 = array(18,19,20);
$arr3 = array('man','man','women');
$arr4 = array_map(function($a,$b,$c){
return [$a,$b,$c];
},$arr1,$arr2,$arr3);
print_r($arr4)
?>
运行结果:
Array
(
[0] => Array
(
[0] => 张三
[1] => 18
[2] => man
)
[1] => Array
(
[0] => 李四
[1] => 19
[2] => man
)
[2] => Array
(
[0] => 王五
[1] => 20
[2] => women
)
)
场景二:一维数组的数据结构为key =>[value1,value2,…,value3];
这种就是前台的input中的name属性是一个数组,比如插入多条数据,后台接收的时候就要拆分数据。
<input type='text' name='productNo[]' />
<input type='text' name='productName[]' />
<input type='text' name='incentiveStandard[]' />
<input type='text' class='withdrawTime' name='effectiveDateStart[]' />
<input type='text' class='withdrawTime' name='effectiveDateEnd[]' />
'name' => array('张三','李四','王五'),
'age' => array(18,19,20),
'sex' => array('man','man','women')
<?php
$data =array(
'name' => array('张三','李四','王五'),
'age' => array(18,19,20),
'sex' => array('man','man','women')
);
$arr = [];
$i = 0;
foreach ($data as $k1 => $v1){
$j = 0;
foreach ($v1 as $k2 => $v2){
$arr[$j][$k1] = $v2;
$j++;
}
$i++;
}
print_r($arr)
?>
运行结果:
Array
(
[0] => Array
(
[name] => 张三
[age] => 18
[sex] => man
)
[1] => Array
(
[name] => 李四
[age] => 19
[sex] => man
)
[2] => Array
(
[name] => 王五
[age] => 20
[sex] => women
)
)
版权声明
本文为[fusheng-fate]所创,转载请带上原文链接,感谢
https://blog.csdn.net/lgb1997/article/details/114808788
边栏推荐
猜你喜欢

Jiugong magic square - the 8th Lanqiao provincial competition - group C (DFS and comparison of all magic square types)

On the use of constant pointer and pointer constant -- exercise (record)

QT drawpixmap and DrawImage blur problem
![[untitled] Notepad content writing area](/img/0a/4a3636025c3e0441f45c99e3c67b67.png)
[untitled] Notepad content writing area

Use of qwbengneview and qwebchannel.

Find the number of "blocks" in the matrix (BFS)

Fast application fuzzy search

Some pits used by uni

Deep learning object detection

Hongji micro classroom | cyclone RPA's "flexible digital employee" actuator
随机推荐
Relative reference and absolute reference of Excel
Map对象 map.get(key)
7-10 longest symmetric substring (25 points) (violence problem solution) C language
Pytorch deep learning practice_ 11 convolutional neural network
Executable program execution process
IPI interrupt
Common interview questions - 4 (MySQL)
Summary of redis classic interview questions 2022
Batch import of orange single micro service
Duplicate key update in MySQL
Establish excel bookkeeping book through setting context menu
Dwsurvey is an open source questionnaire system. Solve the problem that cannot be run and modify the bug.
Frequently asked interview questions - 1 (non technical)
Introduction to qqueue
Radar equipment (greedy)
CMake基础教程(39)pkgconfig
No.1.#_6 Navicat快捷键
‘EddiesObservations‘ object has no attribute ‘filled‘
第36期《AtCoder Beginner Contest 248 打比赛总结》
Object to map