阵列排列,同时保持标题

时间:2016-06-14 22:59:53

标签: php arrays recursion multidimensional-array

我有一个包含任意数量元素的数组,也允许它是一个多维数组。我对这种数组数据的测试示例是:

$arr = array(
         array('Material-A', 'Material-B'),
         array('Profile-A', 'Profile-B', 'Profile-C'),
         array('Thread-A', 'Thread-B'),
         // ... any number of elements
        );

从这个多维数组中,我需要创建一个以下列格式呈线性的单个数组。

$arrFormated = array(
                 'Material-A',
                 'Material-A_Profile-A',
                 'Material-A_Profile-A_Thread-A',
                 'Material-A_Profile-A_Thread-B',
                 'Material-A_Profile-A_Thread-C',
                 'Material-A_Profile-B',
                 'Material-A_Profile-B_Thread-A',
                 'Material-A_Profile-B_Thread-B',
                 'Material-A_Profile-B_Thread-C',
                 'Material-A_Profile-C',
                 'Material-A_Profile-C_Thread-A',
                 'Material-A_Profile-C_Thread-B',
                 'Material-A_Profile-C_Thread-C',
                 'Material-B',
                 'Material-B_Profile-A',
                 'Material-B_Profile-A_Thread-A'
                 // Repeat similar pattern found above, etc...
               );

对于递归函数,迄今为止我能够提出的最好的函数如下:

    private function showAllElements($arr)
    {
        for($i=0; $i < count($arr); $i++)
        {
            $element = $arr[$i];
            if (gettype($element) == "array") {
                $this->showAllElements($element);
            } else {
                echo $element . "<br />";
            }
        }
    }

然而,这段代码并没有接近产生我想要的结果。上述代码的结果是。

Material-A
Material-B
Profile-A
Profile-B
Profile-C
Thread-A
Thread-B

有人可以帮我解决这个函数的递归方面,这样我可以得到我想要的结果吗?

1 个答案:

答案 0 :(得分:1)

我一般建议考虑你想要递归的。您尝试在每个递归步骤中使用当前元素,但您的方法需要在每个递归步骤中查看原始Array的 next 数组元素。在这种情况下,将索引传递给递归函数会更有用,因为当前元素&#39; ($arr中的showAllElements($arr))没有帮助。

我认为这段代码应该这样做:

$exampleArray = array(
         array('Material-A', 'Material-B'),
         array('Profile-A', 'Profile-B', 'Profile-C'),
         array('Thread-A', 'Thread-B','Thread-C'),
         // ... any number of elements
        );

class StackOverflowQuestion37823464{
    public $array;
    public function dumpElements($level = 0 /* default parameter: start at first element if no index is given */){
        $return=[];
        if($level==count($this->array)-1){ 
            $return=$this->array[$level]; /* This is the anchor of the recursion. If the given index is the index of the last array element, no recursion is neccesarry */
        }else{
            foreach($this->array[$level] as $thislevel) { /* otherwise, every element of the current step will need to be concatenated... */
                $return[]=$thislevel;
                foreach($this->dumpElements($level+1) as $stringifyIt){ /*...with every string from the next element and following elements*/
                    $return[]=$thislevel.'_'.$stringifyIt;
                }
            }
        }
        return $return;
    }
}
$test=new StackOverflowQuestion37823464();
$test->array=$exampleArray;
var_dump($test->dumpElements());