数组转换

时间:2013-01-09 13:13:00

标签: php

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 个答案:

答案 0 :(得分:5)

for($i=0;$i<count($yourArray);$i++)
{
    $yourArray[$i] = $yourArray[$i][0];
}

答案 1 :(得分: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);
}

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

答案 2 :(得分:1)

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

$output = array();
array_walk_recursive($input, function($element) use (&$output){
    $output[] = $element;
});

答案 3 :(得分:0)

展平嵌套数组的功能:

function flatten_array(array $array) {
    return iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveArrayIterator($array)),false);
}