2017-12-07 1 views
1

내 뇌가 천천히 시작하는 것 같아요,하지만이 테이블에서 내 결과의 재귀 배열을 얻으려면 함께 테이블에서 간단한 재귀를 가져옵니다 :id_parent

:

|id|int(11)| 
|name|varchar(150)| 
|type|tinyint(4)| 
|id_parent|mediumint(9)| 

|1|Marque forte|1|0| 
|2|Communication|2|1| 
|3|Respect du CI|0|2| 
|4|Stratégie digitale|0|2 
|5|Expérience de marque|2|1| 
|6|Gastronomie|0|5| 

나는 배열처럼 원하는

array('id' => array('name' => $name, 'childs' => array(...), 'id' => ...) 

데이터 배열을 하나의 배열로 주문하고 싶습니다.

는 지금까지이 시도 :

나에게 이상한 결과를 제공
// returns array of objects - codeigniter result 
$dbResult = $this->db->get_where('table')->result(); 

// will contains the array 
$this->data['tree'] = array(); 
$this->getRecursive(0, 0, $dbResult); 
.... 

private function getRecursive($parent, $niveau, $result) 
{ 
    foreach ($result AS $row) 
    { 
     if ($parent == $row->id_parent) 
     { 
      $this->data['tree'][] = array($row->name => $this->getRecursive($row->id, ($niveau + 1), $result)); 
     } 
    } 
} 

이 ...

답변

1

이 시도하십시오. 나는 시험을 위해 $source 배열을 사용했다. 내 결과 집합을 실제 데이터베이스에서 가져 오지 않았으므로 코드를 사용하여 코드를 조정하기 위해 일부 변경해야합니다 (예 : $row['id_parent']에서 $row->id_parent 등으로 변경). 그러나 개념 상으로 효과가 있습니다.

<?php 
     $source = [ 
       ['name' => 'A', 'id' => 1, 'id_parent' => 0], 
       ['name' => 'B', 'id' => 2, 'id_parent' => 1], 
       ['name' => 'C', 'id' => 3, 'id_parent' => 1], 
       ['name' => 'D', 'id' => 4, 'id_parent' => 2], 
       ['name' => 'E', 'id' => 5, 'id_parent' => 3], 
       ['name' => 'F', 'id' => 5, 'id_parent' => 0], 
     ]; 


     function getRecursive($source, $parent) { 
      $result = []; 
      foreach ($source as $row) { 
       if ($parent == $row['id_parent']) { 
        $result[] = [ 
           'id' => $row['id'], 
           'name' => $row['name'], 
           'childs' => getRecursive($source, $row['id']) 
          ]; 
         } 
       } 
       return $result; 
     } 

     print_r(getRecursive($source, 0)); 
+0

아주 좋은 감사를해야 다음! 나도 모르겠다. 내 뇌는 재귀적일 때 꺼져있다 .. –

+0

당신은 환영합니다 :) –

0

당신은 여기에 재귀가 필요하지 않음 - 작업을

$arrTreeById = []; 
//$arrData = $this->db->get_where('table')->result_array(); 

$arrData = [ 
    [ 
     'id' => 1, 
     'name' => 'Marque forte', 
     'type' => 2, 
     'id_parent' => 0 
    ], 
    [ 
     'id' => 2, 
     'name' => 'Communication', 
     'type' => 2, 
     'id_parent' => 1 
    ], 
    [ 
     'id' => 3, 
     'name' => 'Respect du CI', 
     'type' => 0, 
     'id_parent' => 2 
    ], 
    [ 
     'id' => 4, 
     'name' => 'Stratégie digitale', 
     'type' => 0, 
     'id_parent' => 2 
    ], 
    [ 
     'id' => 5, 
     'name' => 'Expérience de marque', 
     'type' => 2, 
     'id_parent' => 1 
    ], 
    [ 
     'id' => 6, 
     'name' => 'Gastronomie', 
     'type' => 0, 
     'id_parent' => 5 
    ], 
]; 

foreach($arrData AS $arrItem) 
{ 
    $arrTreeById[$arrItem['id']] = $arrItem; 
} 

foreach($arrTreeById AS &$arrItem) 
{ 
    if (isset($arrTreeById[$arrItem['id_parent']])) 
    { 
     $arrTreeById[$arrItem['id_parent']]['arrChilds'][] = &$arrItem; 
    } 
    if ($arrItem['id_parent'] == 0) $intStartingKey = $arrItem['id']; 
} 

print_r($arrTreeById[$intStartingKey]); 
관련 문제