当前位置:网站首页>多个一维数组拆分合并为二维数组
多个一维数组拆分合并为二维数组
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
边栏推荐
猜你喜欢
2 - software design principles
Three methods of list rendering
[no title] Click the classification jump page to display the details
QSS, qdateedit, qcalendarwidget custom settings
Some pits used by uni
Fast application fuzzy search
Batch import of orange single micro service
Pilotage growth · ingenuity empowerment -- yonmaster developer training and pilotage plan is fully launched
SQL statement simple optimization
Excel sets row and column colors according to cell contents
随机推荐
Fast application fuzzy search
Golang通过exec模块实现Ping连通性检测案例
Typescript interface & type rough understanding
No.1.#_6 Navicat快捷键
C, class library
solidity合约DOS攻击
MySQL query uses \ g, column to row
refused connection
The QT debug version runs normally and the release version runs crash
The main difference between pointer and reference
Usage and difference of shellexecute, shellexecuteex and winexec in QT
Differences between auto and decltype inference methods (learning notes)
Introduction to qqueue
JS number capitalization method
数据安全入门产品——数据库审计系统详解
Strategies to improve Facebook's touch rate and interaction rate | intelligent customer service helps you grasp users' hearts
Breadth first search topics (BFS)
Write the declaration of a function to return the reference of the array, and the array contains 10 string objects (notes)
Use of qwbengneview and qwebchannel.
提升独立站转化率攻略 | 挽回弃购用户