2014-10-03 3 views
0

надеюсь кто-то может помочь. У меня есть этот бесконечный массив, означающий, что я могу продолжать добавлять к нему детей. Я хочу, чтобы отобразить в навигационной цепочке следующим образом:PHP Breadcrumb с бесконечными детьми

Категория 1> Sub Cat 1> и т.д.> и т.д. Категория 1> Sub Cat 2> и т.д.> и т.д.

Категория 2> Sub Cat 1> и т.д.> и т.д. Категория 2> Подкатегория 2> и т. Д.> И т. Д.

Возможно ли это с моей текущей структурой данных ?! Благодаря!

Array 
(
    [Category 1] => Array 
     (
      [title] => Category 1 
      [id] => 1 
      [parentID] => 0 
      [children] => Array 
       (
        [0] => Array 
         (
          [title] => Child of Category 1 
          [id] => 3 
          [parentID] => 1 
          [children] => Array 
           (
           ) 

         ) 

        [1] => Array 
         (
          [title] => Child of Category 1 
          [id] => 4 
          [parentID] => 1 
          [children] => Array 
           (
           ) 

         ) 

       ) 

     ) 

    [Category 2] => Array 
     (
      [title] => Category 2 
      [id] => 2 
      [parentID] => 0 
      [children] => Array 
       (
        [0] => Array 
         (
          [title] => Child of Category 2 
          [id] => 5 
          [parentID] => 2 
          [children] => Array 
           (
           ) 

         ) 

        [1] => Array 
         (
          [title] => Child of Category 2 
          [id] => 6 
          [parentID] => 2 
          [children] => Array 
           (
           ) 

         ) 

       ) 

     ) 

) 
+0

Да. Вам нужна какая-то рекурсивная функция. –

ответ

0

Как эшши сказал, да, вы можете это сделать.

Я взял на себя смелость попробовать это для вас и смог решить вашу проблему.

<?php 

$data = array(
    "Category 1" => array(
      "title" => "Category 1", 
      "id" => 1, 
      "parentID" => 0, 
      "children" => array(
       "0" => array(
        "title" => "Child of Category 1", 
        "id" => 3, 
        "parentID" => 1, 
        "children" => array() 
       ), 
       "1" => array(
        "title" => "Child of Category 1", 
        "id" => 4, 
        "parentID" => 1, 
        "children" => array() 
       ) 
      ) 
     ), 
    "Category 2" => array(
     "title" => "Category 2", 
     "id" => 2, 
     "parentID" => 0, 
     "children" => array(
      "0" => array(
       "title" => "Child of Category 2", 
       "id" => 5, 
       "parentID" => 2, 
       "children" => array() 
      ), 
      "1" => array(
       "title" => "Child of Category 2", 
       "id" => 6, 
       "parentID" => 2, 
       "children" => array() 
      ) 
     ) 
    ) 
); 


function recurse($data) { 
    $return = array(); 
    $i = 0; 

    foreach ($data as $item) { 
     $i++; // Add to the counter so we can get the proper children associated to the parent 

     $temp = array("title" => "", "id" => 0, "parentID" => 0, "children" => array()); 

     // Define the title of the link 
     if (isset($item['title']) && $item['title'] != "") $temp['title'] = $item['title']; 

     // Define the ID of the link 
     if (isset($item['id']) && $item['id'] != "") $temp['id'] = $item['id']; 

     // Define the parent ID of the link 
     if (isset($item['parentID']) && $item['parentID'] != "") $temp['parentID'] = $item['parentID']; 

     // Define the link 
     $return[$i]['link'] = $temp['title']." - (ID: ".$temp['id'].") - (Parent ID: ".$temp['parentID'].")"; 

     // Define the children of the link 
     if (isset($item['children']) && is_array($item['children']) && !empty($item['children'])) { 
      $return[$i]['children'] = recurse($item['children']); 
      continue; 
     } 
    } 

    return $return; 
} 

function flatten($array = array()) { 
    // Return the given parameter as an array if it isn't one 
    if (!is_array($array)) return array($array); 

    $result = array(); // Initialize the result array 

    // Loop through all of the results, merging them and making them single-dimensional 
    foreach ($array as $value) $result = array_merge($result, flatten($value)); 

    return $result; // Return! 
} 

function define_breadcrumbs($data = array()) { 
    $links = array(); 

    // Loop through all of the data items (single-dimensional) and define the breadcrumbs 
    // with that information 
    foreach (recurse($data) as $item) $links[] = implode(" > ", flatten($item)); 

    return $links; // Return! 
} 

echo "<pre>"; 
print_r(define_breadcrumbs($data)); 
echo "</pre>"; 
Смежные вопросы