2016-12-06 70 views
0

我有这样获取所有的行内数据

Array 
(
    [0] => Array 
     (
      [id] => 1 
      [category_name] => One 
      [parent_id] => 
      [children_recursive] => Array 
       (
       ) 
     ) 
    [1] => Array 
     (
      [id] => 2 
      [category_name] => Two 
      [parent_id] => 
      [children_recursive] => Array 
       (
       ) 
     ) 
    [2] => Array 
     (
      [id] => 3 
      [category_name] => Three 
      [parent_id] => 
      [children_recursive] => Array 
       (
        [0] => Array 
         (
          [id] => 17 
          [category_name] => Three and one 
          [parent_id] => 3 
          [children_recursive] => Array 
           (
           ) 
         ) 
        [1] => Array 
         (
          [id] => 19 
          [category_name] => Three and two 
          [parent_id] => 3 
          [children_recursive] => Array 
           (
            [0] => Array 
             (
              [id] => 21 
              [category_name] => Three and two and one 
              [parent_id] => 19 
              [children_recursive] => Array 
               (
               ) 
             ) 
           ) 
         ) 
        [2] => Array 
         (
          [id] => 20 
          [category_name] => Three and three 
          [parent_id] => 3 
          [children_recursive] => Array 
           (
           ) 
         ) 
       ) 
     ) 
    [3] => Array 
     (
      [id] => 4 
      [category_name] => Four 
      [parent_id] => 
      [children_recursive] => Array 
       (
        [0] => Array 
         (
          [id] => 18 
          [category_name] => Four and one 
          [parent_id] => 4 
          [children_recursive] => Array 
           (
           ) 
         ) 
       ) 
     ) 
) 

数组我想要什么,从这个数组

One 
Two 
Three 
Three >> Three and one 
Three >> Three and two 
Three >> Three and two >> Three and two and one 
Three >> Three and three 
Four 
Four >> Four and one 

我已经试过

$category = myarray; 
renderNode($category); 

function renderNode($node) { 
     foreach($node as $cat){ 
      echo $cat['category_name'].'<br >'; 
      if(!empty($cat[children_recursive'])){ 
       renderNode($cat[children_recursive']); 
      } 
     } 
    } 

而我的输出是

One 
Two 
Three 
Three and one 
Three and two 
Three and two and one 
Three and three 
Four 
Four and one 

编辑 这里是使用var_export所以你可以挂号费并粘贴

array (
    0 => 
    array (
    'id' => 1, 
    'category_name' => 'One', 
    'parent_id' => NULL, 
    'children_recursive' => 
    array (
    ), 
), 
    1 => 
    array (
    'id' => 2, 
    'category_name' => 'Two', 
    'parent_id' => NULL, 
    'children_recursive' => 
    array (
    ), 
), 
    2 => 
    array (
    'id' => 3, 
    'category_name' => 'Three', 
    'parent_id' => NULL, 
    'children_recursive' => 
    array (
     0 => 
     array (
     'id' => 17, 
     'category_name' => 'Three and one', 
     'parent_id' => 3, 
     'children_recursive' => 
     array (
     ), 
    ), 
     1 => 
     array (
     'id' => 19, 
     'category_name' => 'Three and two', 
     'parent_id' => 3, 
     'children_recursive' => 
     array (
      0 => 
      array (
      'id' => 21, 
      'category_name' => 'Three and two and one', 
      'parent_id' => 19, 
      'children_recursive' => 
      array (
      ), 
     ), 
     ), 
    ), 
     2 => 
     array (
     'id' => 20, 
     'category_name' => 'Three and three', 
     'parent_id' => 3, 
     'children_recursive' => 
     array (
     ), 
    ), 
    ), 
), 
    3 => 
    array (
    'id' => 4, 
    'category_name' => 'Four', 
    'parent_id' => NULL, 
    'children_recursive' => 
    array (
     0 => 
     array (
     'id' => 18, 
     'category_name' => 'Four and one', 
     'parent_id' => 4, 
     'children_recursive' => 
     array (
     ), 
    ), 
    ), 
), 
) 
+0

'var_dump'对于表示源数据没有用处,请尝试使用'var_export',这样可以复制和粘贴数组。 – sevavietl

+0

@sevavietl我使用var_export来添加我的数组。看到我更新的问题 –

回答

1

类似的东西全数组列表:

function getValues($array, $prefix = '') 
{ 
    $values = []; 

    foreach ($array as $value) { 
     $values[] = $prefix . $value['category_name']; 

     if (!empty($value['children_recursive'])) { 
      $values = array_merge($values, getValues($value['children_recursive'], $prefix . $value['category_name'] . ' >> ')); 
     } 
    } 

    return $values; 
} 

implode('<br>', getValues($array)); 
+0

array_merge():参数#2不是数组 –

+0

http://sandbox.onlinephpfunctions.com/code/3a561ba875b69835c886569ad529a97bf9d38d7e – vuryss

+0

它是我的错误。我在将来循环内写入return语句:P –

0
$category = myarray; 
renderNode($category); 

function renderNode($parent_category_name = '' ,$node) { 
     foreach($node as $cat){ 
      echo $parent_category_name.$cat['category_name'].'<br >'; 
      if(!empty($cat[children_recursive'])){ 
       renderNode($parent_category_name .$cat['category_name'] ." >> ", $cat[children_recursive']); 
      } 
     } 
    } 
+0

,您可能希望添加关于您共享的代码作为答案的一些上下文(注释)。 FYI –

1

您可以使用RecursiveArrayIterator。你需要给它这样的扩展:

class RecursiveChildrenIterator extends RecursiveArrayIterator 
{ 
    public function hasChildren() 
    { 
     return !empty($this->current()['children_recursive']); 
    } 

    public function getChildren() 
    { 
     return new static($this->current()['children_recursive']); 
    } 
} 

有了这个类,你可以简单地用一个foreach循环:

$iterator = new RecursiveIteratorIterator(
    new RecursiveChildrenIterator($array), 
    RecursiveIteratorIterator::SELF_FIRST 
); 

$result = []; 
$current = []; 
foreach ($iterator as $item) { 
    $current[$iterator->getDepth()] = $item['category_name']; 

    if (!$iterator->hasChildren()) { 
     $result[] = implode(
      '>>', 
      array_slice($current, 0, $iterator->getDepth() + 1) 
     ); 
    } 
} 

采取传递给RecursiveIteratorIteratorRecursiveIteratorIterator::SELF_FIRST标志的通知。

这里是working demo

增加:

其实我误读你期望的数组,所以要得到你想要删除的条件之一:

foreach ($iterator as $item) { 
    $current[$iterator->getDepth()] = $item['category_name']; 

    $result[] = implode(
     '>>', 
     array_slice($current, 0, $iterator->getDepth() + 1) 
    ); 
} 

这里是working demo

您可以阅读更多关于iterators