如何在数组中存储递归函数返回的值?

时间:2016-07-20 08:56:55

标签: php arrays recursion

我有一个嵌套数组 -

$array1 = array(
        array(
            '#type' => 'rev',
            '#rev' => 0,
            '#rev_info' => array(
                'status' => 'available',
                'default' => FALSE,
                'open_rev' => FALSE,
                'conflict' => FALSE,
            ),
            'children' => array(
                array(
                    '#type' => 'rev',
                    '#rev' => 1,
                    '#rev_info' => array(
                        'status' => 'available',
                        'default' => FALSE,
                        'open_rev' => FALSE,
                        'conflict' => FALSE,
                    ),
                    'children' => array(
                        array(
                            '#type' => 'rev',
                            '#rev' => 2,
                            '#rev_info' => array(
                                'status' => 'available',
                                'default' => FALSE,
                                'open_rev' => TRUE,
                                'conflict' => TRUE,
                            ),
                            'children' => array(),
                        ),
                        array(
                            '#type' => 'rev',
                            '#rev' => 3,
                            '#rev_info' => array(
                                'status' => 'available',
                                'default' => FALSE,
                                'open_rev' => FALSE,
                                'conflict' => FALSE,
                            ),
                            'children' => array(
                                array(
                                    '#type' => 'rev',
                                    '#rev' => 4,
                                    '#rev_info' => array(
                                        'status' => 'available',
                                        'default' => TRUE,
                                        'open_rev' => TRUE,
                                        'conflict' => FALSE,
                                    ),
                                    'children' => array(),
                                )
                            )
                        )
                    )
                ),
                array(
                    '#type' => 'rev',
                    '#rev' => 5,
                    '#rev_info' => array(
                        'status' => 'available',
                        'default' => FALSE,
                        'open_rev' => TRUE,
                        'conflict' => TRUE,
                    ),
                    'children' => array(),
                )
            )
        )
    );

    $x = $this->creator($array1);
    print_r($x);

我需要将所有 #rev 值存储在数组中。我已经编写了以递归方式循环遍历此数组的代码,但最终它只返回 0 。 我写的代码是:

public function creator($arr) {
    foreach ($arr as $value) {
        $x = $value['#rev'];
        $storage[$x] = $x;
        if (count($value['children'])) {
            $this->creator($value['children']);
        }
    }
}

我希望creator函数返回一个值为0到5的数组,但它只返回0。

我犯错的地方。请告诉我。

2 个答案:

答案 0 :(得分:0)

您没有从函数返回任何值。您需要从函数返回一些值,以将该值存储在变量中:

public function creator($arr) {

      $storage  = array();    

    foreach ($arr as $value) {
        $x = $value['#rev'];
        $storage[$x] = $x;
        if (count($value['children'])) {
            return creator($value['children']);
        }
    }
   return $storage;
}

或者你可以做任何你正在做的事情但只是通过引用来调用这个值:

    creator(&$array1);
print_r($array1);

答案 1 :(得分:0)

试试这个:

function creator ($arr){
    if(!is_array($arr)) return []; //safeguard in case a non-array is supplied
    $results = []; //will be filled with the #rev values
    foreach($arr as $value){
        //if #rev is found, add its value to results
        if(isset($value['#rev'])) $results[]=$value['#rev'];

        //if children are found, process them recursively and merge the
        // sub-results into the main results array
        if(isset($value['children'])) 
            array_merge($results, creator($value['children']));

    }
    return $results;
};