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



我有一个嵌套数组-

$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。

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

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

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

试试这个:

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;
};

最新更新