downloadTemp2014 2016-01-28 13:53
浏览 44

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

I am reading my css folder and received the below array.

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
)

Now I want these like in below format.

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

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

I dont know how to handle the recursive array for the folders.

  • 写回答

1条回答 默认 最新

  • douhuai4155 2016-01-28 15:06
    关注

    Ok, here's a solution with a recursive call of a function.

    Code

        <?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;
            }
        ?>
    

    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"
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等