php-将二维数组转换为单维数组

时间:2016-01-28 13:53:58

标签: php arrays

我正在阅读我的css文件夹并收到以下数组。

Array
(
    [FormValidation] => Array
        (
            [0] => formValidation.css
        )

    [0] => bootstrap-theme.css
    [1] => bootstrap-theme.min.css
    [2] => bootstrap.css
    [3] => bootstrap.min.css
    [4] => component.css
    [5] => custom.css
    [6] => custom_11_1_backup.css
    [7] => datepicker.css
    [8] => dropkick.css
    [9] => easy-responsive-tabs.css
    [10] => jquery.bootstrap-touchspin.css
    [11] => jquery.fileupload.css
    [12] => jquery.mCustomScrollbar.css
    [13] => jquery.noty.css
    [14] => noty_theme_default.css
    [15] => owl.carousel.css
    [16] => owl.theme.css
    [17] => owl.theme.default.min.css
    [18] => print_invoice.css
    [slider] => Array
        (
            [0] => AjaxLoader.gif
            [1] => owl.theme.css
        )

    [19] => validationEngine.jquery.css
)

现在我想要这些格式如下。

Array
(
    [0] => Array
        (
            [path] => css/FormValidation/formValidation.css
            [name] => formValidation.css
        )

    [1] => Array
        (
            [path] => css/bootstrap-theme.css
            [name] => bootstrap-theme.css
        )
)

我不知道如何处理文件夹的递归数组。

1 个答案:

答案 0 :(得分:0)

好的,这是一个函数递归调用的解决方案。

代码

    <?php
        $data = array(
            'FormValidation' => array (
                   0 => 'formValidation.css'
            ),
            0 => 'bootstrap-theme.css',
            1 => 'bootstrap-theme.min.css',
            2 => 'bootstrap.css',
            3 => 'bootstrap.min.css',
            4 => 'component.css',
            5 => 'custom.css',
            'slider' => array (
                0 => 'AjaxLoader.gif',
                1 => 'owl.theme.css',
            ),
            19 => 'validationEngine.jquery.css',
        );

        $initpath = 'css';
        echo '<b>INPUT</b><pre>'; var_dump($data); echo '</pre>';    // for testing purpose only
        $result = extractStyleSheets($data, $initpath);
        echo '<b>RESULT</b><pre>'; var_dump($result); echo '</pre>'; // for testing purpose only

        function extractStyleSheets($input,$path='') {
            $result = array();
            $success = true;
            foreach($input as $key => $item) {
                if (is_array($item)) { // It's an array, so we have a sub-level
                    // Recursive call of function and merging the arrays
                    $result = array_merge(
                        $result, 
                        extractStyleSheets($item,$path . (empty($path) ? '' : '/') . $key)
                    );
                } else {
                    // For style sheets only do
                    if (preg_match('/(.*?\.css)/i', $item, $regs)) {
                        // Push a sub-array to the result array
                        $result[] = array(
                            'path' => $path . '/' . $item,
                            'name' => $item
                            );
                    }
                }
            }
            return $result;
        }
    ?>

结果

array(9) {
    [0]=> array(2) { 
        ["path"]=> string(37) "css/FormValidation/formValidation.css"
        ["name"]=> string(18) "formValidation.css"
    }
    [1]=> array(2) {
        ["path"]=> string(23) "css/bootstrap-theme.css"
        ["name"]=> string(19) "bootstrap-theme.css"
    }
    [2]=> array(2) {
        ["path"]=> string(27) "css/bootstrap-theme.min.css"
        ["name"]=> string(23) "bootstrap-theme.min.css"
    }
    [3]=> array(2) {
        ["path"]=> string(17) "css/bootstrap.css"
        ["name"]=> string(13) "bootstrap.css"
    }
    [4]=> array(2) {
        ["path"]=> string(21) "css/bootstrap.min.css"
        ["name"]=> string(17) "bootstrap.min.css"
    }
    [5]=> array(2) {
        ["path"]=> string(17) "css/component.css"
        ["name"]=> string(13) "component.css"
    }
    [6]=> array(2) {
        ["path"]=> string(14) "css/custom.css"
        ["name"]=> string(10) "custom.css"
    }    
    [7]=> array(2) {
        ["path"]=> string(24) "css/slider/owl.theme.css"
        ["name"]=> string(13) "owl.theme.css"
    }
    [8]=> array(2) {
        ["path"]=> string(31) "css/validationEngine.jquery.css"
        ["name"]=> string(27) "validationEngine.jquery.css"
    }
}