记录一下工作中遇到的数据拆分问题。
场景一:
一维数组的结构为[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
)
)