如何在PHP中更改数组格式
我想从更改数组格式如何在PHP中更改数组格式,php,arrays,format,resultset,Php,Arrays,Format,Resultset,我想从更改数组格式 Array ( [profileImg] => Array ( [name] => Array ( [0] => index.jpg [1] => index1.jpg ) [type] => Array
Array
(
[profileImg] => Array
(
[name] => Array
(
[0] => index.jpg
[1] => index1.jpg
)
[type] => Array
(
[0] => image/jpeg
[1] => image/jpeg
)
[tmp_name] => Array
(
[0] => D:\xampp\tmp\php714F.tmp
[1] => D:\xampp\tmp\php714F.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
)
[size] => Array
(
[0] => 13543
[1] => 23543
)
)
)
到
我从我的表格中得到这个
<input name="profileImg[]" type="file" />
如果您有任何帮助,我们将不胜感激,谢谢这应该行得通
$result = [];
foreach($_FILES['profileImg'] as $k => $d){
foreach($d as $key => $val){
$result[$key][$k] = $d[$key];
}
}
print\r($result)代码>返回
Array
(
[0] => Array
(
[name] => index.jpg
[type] => image/jpeg
[tmp_name] => D:\xampp\tmp\php714F.tmp
[error] => 0
[size] => 13543
)
[1] => Array
(
[name] => index1.jpg
[type] => image/jpeg
[tmp_name] => D:\xampp\tmp\php714F.tmp
[error] => 0
[size] => 23543
)
)
这就是你想要的
<?php
$result = array();
foreach ($list['profileImg'] as $key => $keyList)
{
foreach ($keyList as $index => $value)
{
$result[$index][$key] = $value;
}
}
print_r($result);
应该这样做。您已经尝试了什么?我尝试了foreach,但找不到方法。您的代码有一些错误,但经过一些更正后工作正常。感谢Ben。这是正确的语法,从PHP 5.4开始从PHP 5.4开始,您还可以使用短数组语法,它将数组()替换为[]。
如果您使用的是较旧的版本,则只需将其替换为array()
。我使用的是最新版本,我知道该怎么做。只要通知你和其他访客就行了。不管怎样,谢谢。没有绿色记号也行)嘿,斯莱恩,那是你和本之间的偏袒,因为没有人投票给他,他在你之前回答了一点。啊哈!^ ^,你太好了。
<?php
$result = array();
foreach ($list['profileImg'] as $key => $keyList)
{
foreach ($keyList as $index => $value)
{
$result[$index][$key] = $value;
}
}
print_r($result);
$result = array();
foreach ($_FILES['profileImg'] as $name => $data) {
foreach ($data as $key => $item) {
$result[$key][$name] = $item;
}
}