1
Array ( 
    [0] => Array ( 
        [0] => uploads/AP02A66_31_upload1_1357736699_SeamTrade.php 
    ) 
    [1] => Array ( 
        [0] => uploads/AP02A66_31_upload1_1357736699_SiteController.php 
    ) 
)

如何将上述数组转换为一个索引数组,以便它以如下形式出现,

Array ( 
    [0] => uploads/AP02A66_31_upload1_1357736699_SeamTrade.php  
    [1] => uploads/AP02A66_31_upload1_1357736699_SiteController.php 
)
4

4 回答 4

5
for($i=0;$i<count($yourArray);$i++)
{
    $yourArray[$i] = $yourArray[$i][0];
}
于 2013-01-09T13:15:45.823 回答
2
$sourceArray = array( 
    array('uploads/AP02A66_31_upload1_1357736699_SeamTrade.php'),
    array('uploads/AP02A66_31_upload1_1357736699_SiteController.php'),
);
$newArray = array_map(function ($nestedArray) {
    return $nestedArray[0];
}, $sourceArray);

或另一种方式(将就地执行此操作,因此请注意源数组将被更改):

foreach ($sourceArray as &$element) {
    $element = $element[0];
}

或更灵活的方式 - 如果您的嵌套数组可以包含多个元素:

$newArray = array();
foreach ($sourceArray as $nestedArray) {
    $newArray = array_merge($newArray, $nestedArray);
}

还有很多其他的方法,但我想上面的应该足够了;)

于 2013-01-09T13:18:16.053 回答
1

另一种可能的解决方案,假设您的数组被称为$input

$output = array();
array_walk_recursive($input, function($element) use (&$output){
    $output[] = $element;
});
于 2013-01-09T13:23:42.183 回答
0

扁平化嵌套数组的函数:

function flatten_array(array $array) {
    return iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveArrayIterator($array)),false);
}
于 2013-01-09T13:14:54.170 回答