2015-08-19 19 views
6

Mam dużą tablicę wielowymiarową. I muszę znaleźć z tego konkretną pod-tablicę.
Próbowałem użyć jednej funkcji rekursji, ale w rzeczywistości nie zwraca wartości. Czy ktoś może mi dać inne rozwiązanie.
Oto podgląd tablicy.Uzyskaj konkretną wartość z wielowymiarowej tablicy

Array 
(
    [0] => Array 
     (
      [expanded] => 1 
      [key] => _1 
      [title] => New 
     ) 

    [1] => Array 
     (
      [key] => _2 
      [title] => Home 
     ) 

    [2] => Array 
     (
      [expanded] => 1 
      [key] => _3 
      [title] => Care 
      [children] => Array 
       (
        [0] => Array 
         (
          [expanded] => 1 
          [key] => _4 
          [title] => face 
          [children] => Array 
           (
            [0] => Array 
             (
              [key] => _5 
              [title] => new 
             ) 

            [1] => Array 
             (
              [key] => _6 
              [title] => <strong>face timeline</strong> 
              [data] => Array 
               (
                [url] => http://localhost/patient/face-timeline/ 

                [type] => content 
                [cid] => 2291 
                [timeline] => 0 
               ) 

              [children] => Array 
               (
                [0] => Array 
                 (
                  [key] => _2278 
                  [title] => Post Op Visit 
                 ) 

                [1] => Array 
                 (
                  [key] => _2277 
                  [title] => Surgery 
                 ) 

                [2] => Array 
                 (
                  [key] => _2276 
                  [title] => Pre-Op 
                 ) 

                [3] => Array 
                 (
                  [key] => _2275 
                  [title] => Consultation 
                 ) 

                [4] => Array 
                 (
                  [key] => _2274 
                  [title] => Reseach 
                 ) 

               ) 

             ) 

           ) 

         ) 

       ) 

     ) 

) 

Od tej tablicy chcę to array (poniżej):

Array 
(
    [key] => _6 
    [title] => <strong>face timeline</strong> 
    [data] => Array 
     (
      [url] => http://localhost/patient/face-timeline/ 
      [type] => content 
      [cid] => 2291 
      [timeline] => 0 
     ) 

    [children] => Array 
     (
      [0] => Array 
       (
        [key] => _2278 
        [title] => Post Op Visit 
       ) 

      [1] => Array 
       (
        [key] => _2277 
        [title] => Surgery 
       ) 

      [2] => Array 
       (
        [key] => _2276 
        [title] => Pre-Op 
       ) 

      [3] => Array 
       (
        [key] => _2275 
        [title] => Consultation 
       ) 

      [4] => Array 
       (
        [key] => _2274 
        [title] => Reseach 
       ) 

     ) 

) 

Oto co próbowałem

function recursion($array,$postid) { 

    foreach ($array as $key=>$value) { 

     if((isset($value['data']['cid'])) && ($value['data']['cid'] == $postid)){ 

      $tmp = $value; 
       return $value; 

     } 
     if (is_array($value)) 
     { 

      recursion($value,$postid); 
     } 
    } 

} 

Ta funkcja nie zwraca wartości.
Tutaj $postid jest 2291. Że ja szukam i jestem w stanie wydrukować tej tablicy, ale nie może w stanie zwrócić wartość Tu jest link

+1

Gdzie jest twój kod? – artsylar

+0

W przykładzie z Array proszę również dodać kod tutaj lub w skrzypcach –

Odpowiedz

1

Jeśli chcesz uzyskać tylko konkretnego wykorzystania tej wartości:

function recursive($your_array) 
    { 
     $newArray = []; 

     foreach ($your_array as $key => $val) { 
      if (array_keys($your_array) == 'children') { 
       foreach($val as $key2 => $val3){ 
        $newArray[] = recursive($val3); 
       } 
      } 
     } 
     print_r($newArray); 
    } 
+0

Mam więcej numerów osi czasu i to nie działa. –

+0

@milanpatel Czy to działa teraz? – aldrin27

+0

Przykro mi, to nie działa. :( –

0

ten da ci wynik:

Powiązane problemy