doqrjrc95405 2015-12-26 04:14
浏览 29

循环通过未确定数量的链式数组

I know basic PHP and all I know I've learned on google during the past years. With that said, I'm developing a new website and I'm experimenting with clean URLs for SEO purposes and multi-regional languages compatibility.

So I have this array:

$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'
    )
);

Where $list['requests']['en'][$page][0] is the level of the page in the url and $list['requests']['en'][$page]['next'] is a list of valid pages that can follow. That array is used to validate a URLs, so this is a valid one: www.website.com/services/service2/view

My real problem is that I want to loop throug that array and follow every page in the 'next' list so I have a function to create files like this: en/services.service1.view.php

I alreday created a chain of loops that do the job, but it's not generic. It's limited to the amount of loops I type in myself. This code for example, only runs throug 4 loops:

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;

                            }
                        }
                    }
                }
            }
        }
    }
}

This code saves every unique file name from the array chain. The output would be something like this:

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

I've tried to make generic functions to do this but with no success. Any ideas?

EDIT: I just came up with an idea to simplify the code with an include. Nevermind my variables, the point here is to make a script that perceives the depth of loops itself.

The hole script for the purpose of context:

<?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/>';

}

?>

The loop is included and verifies if it's needed to be included again in itself (___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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大