2016-07-20 3 views
2

Ich habe eine verschachtelte Array -Wie man Werte speichert, die durch rekursive Funktion in einem Array zurückgegeben werden?

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

Ich brauche die in einem Array #REV alle Werte zu speichern. Ich habe den Code geschrieben, der rekursiv über dieses Array läuft, aber am Ende gibt es nur zurück. Der Code, den ich geschrieben habe, ist:

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

Ich mag die creator Funktion ein Array mit Werten von 0 bis 5 zurückzukehren, aber es gibt nur 0.

Wo mache ich den Fehler. Lass es mich wissen, bitte.

Antwort

0

Sie geben keinen Wert von der Funktion zurück. Sie müssen einen Wert aus der Funktion zurück, dass Wert in einer Variablen speichern:

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

Oder Sie können, was Sie tun, tun, sondern nur den Wert als Referenz nennen:

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

Try this:

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