二维数组到多个带键的一维数组
2 dimension array to multiple 1 dimension arrays with keys
我的数组:
Array
(
[patient_id] => Array
(
[0] => 23
[1] => 24
[2] => 25
)
[fullname] => Array
(
[0] => Jhon Sena
[1] => Mary Sena
[2] => Carter Sena
)
[type] => Array
(
[0] => pdf
[1] => pdf
[2] => pdf
)
[textarea_text] => Array
(
[0] => text
[1] => text2
[2] => text3
)
)
我想得到什么:
Array
(
[patient_id] => 23
[fullname] => Jhon Sena
[type] => pdf
[textarea_text] => text
)
Array
(
[patient_id] => 24
[fullname] => Mary Sena
[type] => pdf
[textarea_text] => text2
)
Array
(
[patient_id] => 25
[fullname] => Carter Sena
[type] => pdf
[textarea_text] => text3
)
我一直在尝试使用许多 php flatten array functions pointed here in another question 但我得不到想要的结果,你能给我指出正确的方向吗?
例如这个展平函数:
function flatten($ar) {
$toflat = array($ar);
$res = array();
while (($r = array_shift($toflat)) !== NULL) {
foreach ($r as $v) {
if (is_array($v)) {
$toflat[] = $v;
} else {
$res[] = $v;
}
}
}
return $res;
}
没有键就给我这个结果:
Array
(
[0] => 23
[1] => 24
[2] => 25
[3] => Jhon Sena
[4] => Mary Sena
[5] => Carter Sena
[6] => pdf
[7] => pdf
[8] => pdf
[9] => text
[10] => text2
[11] => text3
)
............................
你可以通过简单的方式做到这一点 foreach()
$fialArray = [];
foreach($array as $key=>$value){
foreach($value as $k=>$val){
$fialArray[$k][$key] = $val;
}
}
print_r($fialArray);
我的数组:
Array
(
[patient_id] => Array
(
[0] => 23
[1] => 24
[2] => 25
)
[fullname] => Array
(
[0] => Jhon Sena
[1] => Mary Sena
[2] => Carter Sena
)
[type] => Array
(
[0] => pdf
[1] => pdf
[2] => pdf
)
[textarea_text] => Array
(
[0] => text
[1] => text2
[2] => text3
)
)
我想得到什么:
Array
(
[patient_id] => 23
[fullname] => Jhon Sena
[type] => pdf
[textarea_text] => text
)
Array
(
[patient_id] => 24
[fullname] => Mary Sena
[type] => pdf
[textarea_text] => text2
)
Array
(
[patient_id] => 25
[fullname] => Carter Sena
[type] => pdf
[textarea_text] => text3
)
我一直在尝试使用许多 php flatten array functions pointed here in another question 但我得不到想要的结果,你能给我指出正确的方向吗?
例如这个展平函数:
function flatten($ar) {
$toflat = array($ar);
$res = array();
while (($r = array_shift($toflat)) !== NULL) {
foreach ($r as $v) {
if (is_array($v)) {
$toflat[] = $v;
} else {
$res[] = $v;
}
}
}
return $res;
}
没有键就给我这个结果:
Array
(
[0] => 23
[1] => 24
[2] => 25
[3] => Jhon Sena
[4] => Mary Sena
[5] => Carter Sena
[6] => pdf
[7] => pdf
[8] => pdf
[9] => text
[10] => text2
[11] => text3
)
............................
你可以通过简单的方式做到这一点 foreach()
$fialArray = [];
foreach($array as $key=>$value){
foreach($value as $k=>$val){
$fialArray[$k][$key] = $val;
}
}
print_r($fialArray);