来自值的PHP数组路径

时间:2017-10-09 13:48:49

标签: php arrays

你好我有这个数组

$ar=array(
'cat','dog','turtle','eagle'
);

我想用这个结果生成另一个数组

$arFinal=array(
[cat,dog,turtle,eagle],
[cat,dog,turtle],
[cat,dog],
[cat],
[dog,turtle,eagle],
[dog,turtle],
[dog],
[turtle,eagle],
[turtle],
[eagle],
);

1 个答案:

答案 0 :(得分:0)

早在你们面前,我的解决方案(来自对你提问的评论):

<?php
    $array = array('cat','dog','turtle','eagle');

    $result = array();
    for($i = 0; $i < count($array); $i++){
        for($l = $i; $l < count($array); $l++){
            $result[] = array_slice($array, $i, count($array)-$l);
        }
    }
?>

输出:

array(10) {
    array(4) { [0]=>  "cat" [1]=>  "dog" [2]=>  "turtle" [3]=>  "eagle" }
    array(3) { [0]=>  "cat" [1]=>  "dog" [2]=>  "turtle" }
    array(2) { [0]=>  "cat" [1]=>  "dog" }
    array(1) { [0]=>  "cat" }
    array(3) { [0]=>  "dog" [1]=>  "turtle" [2]=>  "eagle" }
    array(2) { [0]=>  "dog" [1]=>  "turtle" }
    array(1) { [0]=>  "dog" }
    array(2) { [0]=>  "turtle" [1]=>  "eagle" }
    array(1) { [0]=>  "turtle" }
    array(1) { [0]=>  "eagle" } 
} 

此致
萨姆。