循环遍历未定义的链式数组

时间:2015-12-26 04:14:30

标签: php arrays loops clean-urls

我知道基本的PHP和我所知道的在过去几年里我在Google上学到的知识。 话虽如此,我正在开发一个新网站,我正在尝试使用干净的URL进行搜索引擎优化和多区域语言的兼容性。

所以我有这个数组:

$list['requests']['en'] = array(
    'index' => array(
        0,
        'subscribe',
        'search'
    ),
    'services' => array(
        0,
        'next' => array(
            'service1',
            'service2',
            'service3'
        ),
        'subscribe',
        'search'
    ),
    'service1' => array(
        0,
        'subscribe'
    ),
    'service2' => array(
        1,
        'next' => array(
            'view'
        ),
        'jump' => 1,
        'subscribe',
        'search'
    ),
    'service3' => array(
        1,
        'subscribe',
    ),
    'view' => array(
        2,
        'subscribe'
    ),
    'bad-request' => array(
        0,
        'subscribe'
    )
);

其中$list['requests']['en'][$page][0]是网址中网页的级别,$list['requests']['en'][$page]['next']是可以关注的有效网页列表。 该数组用于验证网址,因此这是一个有效的网址:www.website.com/services/service2/view

我真正的问题是我想循环遍历那个数组并跟随'next'列表中的每一页,所以我有一个函数来创建这样的文件:en / services.service1.view.php

我已经创建了一系列完成工作的循环,但它不是通用的。它仅限于我自己键入的循环数量。例如,此代码仅运行4个循环:

if(isset($list['requests'][$lang][$list_keys[$j]]['next'])){
    for($l = 0; !empty($list['requests'][$lang][$list_keys[$j]]['next'][$l]) && $l < $for_max; $l++){
        $next = $list['requests'][$lang][$list_keys[$j]]['next'][$l];

        $file_next = $file[0].'.'.$next;
        $file[] = $file_next;

        if(isset($list['requests'][$lang][$next]['next'])){
            for($n = 0; !empty($list['requests'][$lang][$next]['next'][$n]) && $n < $for_max; $n++){
                $next_1 = $list['requests'][$lang][$next]['next'][$n];

                $file_next_1 = $file_next.'.'.$next_1;
                $file[] = $file_next_1;

                if(isset($list['requests'][$lang][$next_1]['next'])){
                    for($o = 0; !empty($list['requests'][$lang][$next_1]['next'][$o]) && $o < $for_max; $o++){
                        $next_2 = $list['requests'][$lang][$next_1]['next'][$o];

                        $file_next_2 = $file_next_1.'.'.$next_2;
                        $file[] = $file_next_2;

                        if(isset($list['requests'][$lang][$next_2]['next'])){
                            for($p = 0; !empty($list['requests'][$lang][$next_2]['next'][$p]) && $p < $for_max; $p++){
                                $next_3 = $list['requests'][$lang][$next_2]['next'][$p];

                                $file_next_3 = $file_next_2.'.'.$next_3;
                                $file[] = $file_next_3;

                            }
                        }
                    }
                }
            }
        }
    }
}

此代码保存数组链中的每个唯一文件名。 输出将是这样的:

index.php
services.php
services.service1.php
services.service2.php
services.service2.view.php
services.service3.php
...

我试图制作通用函数来做到这一点,但没有成功。 有什么想法吗?

修改 我想出了一个用include来简化代码的想法。没关系我的变量,这里的要点是制作一个能够感知循环深度的脚本。

用于上下文的洞脚本:

<?php
include 'includes/lists/languages.php';
include 'includes/lists/requests.php';

$for_max = 15;

// create requests files for each language
for($i = 0; isset($list['languages'][$i]) && $i < $for_max; $i++){
    $lang = $list['languages'][$i];
    $requests_keys = array_keys($list['requests'][$lang]);

    echo 'LANG: '.$lang.'<br/>';

    for($j = 0; isset($list['requests'][$lang][$requests_keys[$j]]) && $j < $for_max; $j++){
        if($list['requests'][$lang][$requests_keys[$j]][0] == 0){
            $file = array();
            $file[] = $requests_keys[$j];


            /*   NEXT PART LOOP   */
            $loop = array();
            $depth = 0;
            $part = array();
            $part['next'] = array();
            $part['file'] = array();

            // these vars are unique for the first loop, then it's generic
            $part['next'][$depth-1] = $requests_keys[$j];
            $part['file'][$depth-1] = $file[0];

            // loop through every 'next' part if it exists in the current part
            if(isset($list['requests'][$lang][  $part['next'][$depth-1]  ]['next'])){
                include '___construct_files.recursive.php';
            }


            /*   CREATE REQUEST FILE  */
            for($k = 0; $k < count($file); $k++){
                $path = 'includes/requests/'.$lang.'/'.$file[$k].'.php';

                // string to insert in the file
                include '___construct_files.string.php';

                // processing
                if(!file_exists($path)){
                    file_put_contents($path, $string);
                    echo '<br/># created - '.$path.'<br/>';

                }else if(file_exists($path)){
                    echo '<br/># exists - '.$path.'<br/>';

                }else{
                    echo '<br/># error - '.$path.'<br/>';

                }
            }
        }
    }

    echo '<br/><br/>';

}

?>

包含循环并验证是否需要再次包含它(___ construct_files.recursive.php):

<?php
for($loop[$depth] = 0; !empty($list['requests'][$lang][  $part['next'][$depth-1]  ]['next'][  $loop[$depth]  ]) && $loop[$depth] < $for_max; $loop[$depth]++){
    $part['next'][$depth] = $list['requests'][$lang][  $part['next'][$depth-1]  ]['next'][  $loop[$depth]  ];

    $part['file'][$depth] = $part['file'][$depth-1].'.'.$part['next'][$depth];
    $file[] = $part['file'][$depth];

    // if next part is set, increase depth and include loop
    if(isset($list['requests'][$lang][  $part['next'][$depth]  ]['next'])){
        $depth++;

        include '___construct_files.recursive.php';
    }

}
// decrease depth when higher depth loop ends so the current loop can resume
$depth--;

?>

0 个答案:

没有答案