PHP 中的进程数组


Process array in PHP

我有一个数组:

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) 
           } 
}

请帮助我。

尝试这样的事情。

$newArray = [];
foreach ($oldArray as $key => $value) {
    $arrayKey = key($value);
    $newArray[$arrayKey] = $value[$arrayKey];
}

大概意味着将三维数组变成二维数组,且关键词不能重复。

      //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

我测试了这个,它工作正常

$length = count($myOldArray);
for($i = 0; $i< $length; $i++)
$myNewArray[57+$i] = $myOldArray[$i][57];
您可以使用

array_walk函数

函数计数仅用于知道数组是否有任何元素,因为假设增量为 2,因此在 57 之后需要索引 59。您也可以使用函数或任何其他函数。

$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
        )
)