从multidimentional数组中删除父键

时间:2015-08-14 04:55:54

标签: php arrays multidimensional-array

我有一个多维数组。我想删除父键[0],[1],[2] .....什么是删除多维数组中父键的最佳方法?例如,我有这个代码

Array
(
[0] => Array
    (
        [comment_tN9l43iUjZLNap4Dbkf7w8Whb3] => Array
            (
                [required] => This field is required
            )

    )

[1] => Array
    (
        [checkbox_cNVyw1lV0eVrYdeymth2c90AW] => Array
            (
                [required] => Select Gender
                [minlength] => Please select at least 2 items.
                [maxlength] => Please select no more than 4 items.
            )

    )

[2] => Array
    (
        [checkbox_EM9tkQoZ4YMaPncAPenfi6ltB] => Array
            (
                [required] => This field is required
                [minlength] => Please select at least 1 items.
                [maxlength] => Please select no more than 3 items.
            )

    )

)

但我想要像这样的数组

Array
    (
        [comment_tN9l43iUjZLNap4Dbkf7w8Whb3] => Array
            (
                [required] => This field is required
            )
        [checkbox_cNVyw1lV0eVrYdeymth2c90AW] => Array
            (
                [required] => Select Gender
                [minlength] => Please select at least 2 items.
                [maxlength] => Please select no more than 4 items.
            )
        [checkbox_EM9tkQoZ4YMaPncAPenfi6ltB] => Array
            (
                [required] => This field is required
                [minlength] => Please select at least 1 items.
                [maxlength] => Please select no more than 3 items.
            )

    )

2 个答案:

答案 0 :(得分:4)

您只需通过array_merge致电call_user_func_array即可获得所需的结果。

$newArray = call_user_func_array("array_merge", $oldArray);

您可以看到简化示例here

答案 1 :(得分:0)

你可以试试这个:

$newArray = [];
foreach ($oldArray AS $data) {
    $newArray = array_merge($newArray, $data);
}

有关详情,请参阅array_merge() public function clear_path_cache($uri) { $CI =& get_instance(); $path = $CI->config->item('cache_path'); //path of cache directory $cache_path = ($path == '') ? APPPATH.'cache/' : $path; $uri = $CI->config->item('base_url'). $CI->config->item('index_page'). $uri; $cache_path .= md5($uri); return @unlink($cache_path); } /** * Clears all cache from the cache directory */ public function clear_all_cache() { $CI =& get_instance(); $path = $CI->config->item('cache_path'); $cache_path = ($path == '') ? APPPATH.'cache/' : $path; $handle = opendir($cache_path); while (($file = readdir($handle))!== FALSE) { //Leave the directory protection alone if ($file != '.htaccess' && $file != 'index.html') { @unlink($cache_path.'/'.$file); } } closedir($handle); }