my array:
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
)
)
what i'm triying to get:
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
)
i've been trying with many php flatten array functions pointed here in another questions but i could not get the desired results, can you please point me in the right direction?
For example this flatten function:
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;
}
Throws me this result with no keys:
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
)
.............................
You can do it via simple foreach()
$fialArray = [];
foreach($array as $key=>$value){
foreach($value as $k=>$val){
$fialArray[$k][$key] = $val;
}
}
print_r($fialArray);
Output: https://3v4l.org/qqgp7
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.