我提交的表单只有一个文件名类型的输入:photo[profile][]
。我得到了这个数组:
Array
(
[photo] => Array
(
[name] => Array
(
[profile] => Array
(
[0] => Chrysanthemum.jpg
[1] => Desert.jpg
)
)
[type] => Array
(
[profile] => Array
(
[0] => image/jpeg
[1] => image/jpeg
)
)
[tmp_name] => Array
(
[profile] => Array
(
[0] => C:\xampp\tmp\php9DCC.tmp
[1] => C:\xampp\tmp\php9E0B.tmp
)
)
[error] => Array
(
[profile] => Array
(
[0] => 0
[1] => 0
)
)
[size] => Array
(
[profile] => Array
(
[0] => 879394
[1] => 845941
)
)
)
)
所以,我想要一个递归函数,它将我的数组转换为:
Array
(
[photo] => Array
(
[profile] => Array
(
[0] => Array
(
[name] => Chrysanthemum.jpg
[type] => image/jpeg
[tmp_name] => C:\xampp\tmp\php9DCC.tmp
[error] => 0
[size] => 845941
)
[1] => Array
(
[name] => Desert.jpg
[type] => image/jpeg
[tmp_name] => C:\xampp\tmp\php9DCC.tmp
[error] => 0
[size] => 845941
)
)
)
)
我需要这个,因为我想上传更多照片并为每组文件设置配置。
答案 0 :(得分:1)
没有原生的,只需使用循环。
$result = array();
foreach($_FILES as $key => $value){
foreach($value as $key2 => $value2){
foreach($value2 as $key3 => $value3){
foreach($value3 as $index => $value4){
$result[$key][$key3][$index][$key2] = $value4;
}
}
}
}
答案 1 :(得分:1)
使用array_column
函数(自PHP 5.5起可用)和array_column
函数的解决方案:
if (isset($_FILES['photo'])) {
$quantity = count($_FILES['photo']['size']['profile']);
$photos = array_column($_FILES['photo'], 'profile');
$ordered_list = ['photo' => ['profile' => []]];
for ($i = 0; $i < $quantity; $i++) {
$ordered_list['photo']['profile'][] = array_combine(['name', 'type', 'tmp_error', 'error', 'size'], array_column($photos, $i));
}
}
现在$ordered_list
包含所需的分组数据