我有一个数组:
array(2) {
[0]=> array(1) {
[57]=> array(5) {
["name"]=> string(8) "sky.docx"
["type"]=> string(71) "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
["size"]=> int(14413)
["tmp_name"]=> string(24) "D:\xampp\tmp\php381B.tmp"
["error"]=> int(0)
}
}
[1]=> array(1) {
[57]=> array(5) {
["name"]=> string(50) "11536101_730381813774270_2393648058450493003_n.jpg"
["type"]=> string(10) "image/jpeg"
["size"]=> int(52314)
["tmp_name"]=> string(24) "D:\xampp\tmp\php78F5.tmp"
["error"]=> int(0)
}
}
}
我希望改变格式:
array(2) {
[57]=> array(5) {
["name"]=> string(50) "11401351_729347963877655_7852714736534111540_n.jpg"
["type"]=> string(10) "image/jpeg"
["size"]=> int(45023)
["tmp_name"]=> string(24) "D:\xampp\tmp\php4FDA.tmp"
["error"]=> int(0)
}
[59]=> array(5) {
["name"]=> string(49) "11427212_729926670486451_281410776821596523_n.jpg"
["type"]=> string(10) "image/jpeg"
["size"]=> int(29765)
["tmp_name"]=> string(24) "D:\xampp\tmp\php4FEB.tmp"
["error"]=> int(0)
}
}
请帮帮我。
答案 0 :(得分:0)
尝试这样的事情。
$newArray = [];
foreach ($oldArray as $key => $value) {
$arrayKey = key($value);
$newArray[$arrayKey] = $value[$arrayKey];
}
答案 1 :(得分:0)
可能意味着将三维数组转换为二维数组,而关键字不能重复。
//just change format
$begin = [
[
57 =>[
"name"=> "sky.docx" ,
"type"=> "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ,
"size"=> 14413,
"tmp_name"=> "D:\xampp\tmp\php381B.tmp",
"error"=> 0,
],
],
[
57 =>[
"name"=> "sky.docx",
"type"=> "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"size"=> 14413,
"tmp_name"=> "D:\xampp\tmp\php381B.tmp",
"error"=> 0,
]
]
];
print_r($begin);
$result = [];
foreach ($begin as $key => $value) {
foreach ($value as $k=>$v) {
if(array_key_exists($k, $result)){
$result[$k+2] = $v;
}else{
$result[$k] = $v;
}
}
}
print_r($result);
$begin
:
答案 2 :(得分:0)
我测试了这个,它工作正常
$length = count($myOldArray);
for($i = 0; $i< $length; $i++)
$myNewArray[57+$i] = $myOldArray[$i][57];
答案 3 :(得分:0)
您可以使用array_walk功能
函数count仅用于知道数组是否包含任何元素,因为在57个假设为增量2之后需要索引59.您也可以使用empty或任何其他函数。 / p>
$result = array();
array_walk($array, function($row) use (&$result){
$key=key($row);
$result[(count($result)? $key+2 : $key)]=$row[$key];
});
print_r($result);
测试结果
[akshay@localhost tmp]$ cat test.php
<?php
$array = array (
0 =>
array (
57 =>
array (
'name' => 'sky.docx',
'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'size' => 14413,
'tmp_name' => 'D:
mpp mp\\php381B.tmp',
'error' => 0,
),
),
1 =>
array (
57 =>
array (
'name' => 'sky.docx',
'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'size' => 14413,
'tmp_name' => 'D:
mpp mp\\php381B.tmp',
'error' => 0,
),
),
);
print_r($array);
$result = array();
array_walk($array, function($row) use (&$result){
// Get index
$key=key($row);
// array_key_exists is used since you need index 59 after 57
// assumption is increment 2
$result[(count($result)? $key+2 : $key)]=$row[$key];
});
print_r($result);
?>
<强>输出强>
[akshay@localhost tmp]$ php test.php
Array
(
[0] => Array
(
[57] => Array
(
[name] => sky.docx
[type] => application/vnd.openxmlformats-officedocument.wordprocessingml.document
[size] => 14413
[tmp_name] => D:
mpp mp\php381B.tmp
[error] => 0
)
)
[1] => Array
(
[57] => Array
(
[name] => sky.docx
[type] => application/vnd.openxmlformats-officedocument.wordprocessingml.document
[size] => 14413
[tmp_name] => D:
mpp mp\php381B.tmp
[error] => 0
)
)
)
Array
(
[57] => Array
(
[name] => sky.docx
[type] => application/vnd.openxmlformats-officedocument.wordprocessingml.document
[size] => 14413
[tmp_name] => D:
mpp mp\php381B.tmp
[error] => 0
)
[59] => Array
(
[name] => sky.docx
[type] => application/vnd.openxmlformats-officedocument.wordprocessingml.document
[size] => 14413
[tmp_name] => D:
mpp mp\php381B.tmp
[error] => 0
)
)