2014-03-27 129 views
1

我有字符串形式的「IT/Internet/Web開發/ Ajax」。我分析它,並作出這樣遞歸搜索函數

[{ 
    "name": "IT", 
    "subcategories":[ 
    { 
    "name": "Internet", 
    "subcategories" : [ 
    { 
     "name": "Web Development", 
     "subcategories" : [ 
     { 
     "name":"Ajax" 
     }]}]}] 

JSON對象我這樣做

$input = "IT/Internet/Web Development"; 
$items = explode("/", $input); 

$parent = null; 
$firstObject = null; 
while (count($items)) 
{ 
$object = new StdClass(); 
$item = array_shift($items); 
$object->name = $item; 
if (count($items) == 0) { 
    $object->subcategories=NULL; // I made this null in order to know that this is the last item of the string that comes in 
} 
if ($parent) 
    $parent->subcategories = array($object); 
else 
    $firstObject = $object; 

    $parent = $object; 
} 
array_push($category_collection, $firstObject); //$category_collection is an array 
} 

當另一個字符串進來,例如「IT /互聯網/瀏覽器」創建JSON對象,我想能夠解析在正確的地方作爲互聯網的一個子類別中創建和放置「瀏覽器」的類別,所以後來我的JSON對象看起來像

[{ 
    "name": "IT", 
    "subcategories":[ 
    { 
    "name": "Internet", 
    "subcategories" : [ 
    { 
     "name": "Web Development", 
     "subcategories" : [ 
     { 
      "name":"Ajax" 
     }], 
     { 
     "name":"Browsers" 
     }}]}] 

我有問題,寫一個遞歸函數這隻會循環JSON對象來將所有內容分類在正確的位置。我在做什麼截至目前是

$arrlength = count($category_collection); //count the size of the array 
    $input = "IT/Internet/Browsers"; 
    $items = explode("/",$input); 
    $tempVariable = array_shift($items); 
    $flag = false; 
    for ($x = 0; $x < $arrlength; $x++) { 
     //Here I check if the first a category with that name already exists 
     if ($category_collection[$x]['name'] == $tempVariable) { 
      $flag = true; 

        //Now here is where im having problems doing the recursion to check if the subcategory2 already exists and then if subcategory 3 and so on... 

     } 

    } 

如果有人能指導我將不勝感激

回答

1

這裏是一個全功能的,它應該工作,你可以將其轉換後的JSON,如果你需要:

$categoriesCollection = array(); 

$input = "IT/Internet/Web Development"; 
updateCategoriesCollection(explode('/', $input), $categoriesCollection); 
$input = "IT/Internet/Browsers"; 
updateCategoriesCollection(explode('/', $input), $categoriesCollection); 

function updateCategoriesCollection(array $categoriesList, array &$categoriesCollection) 
{ 
    $name = array_shift($categoriesList); 
    $category = null; 
    foreach ($categoriesCollection as $key => $value) 
    { 
     if ($value->name == $name) 
     { 
      $category = $value; 
      break; 
     } 
    } 
    if (!$category) 
    { 
     $category = new StdClass; 
     $category->name = $name; 
     $categoriesCollection[] = $category; 
    } 

    if (!empty($categoriesList)) 
    { 
     if (empty($category->subcategories)) $category->subcategories = array(); 
     updateCategoriesCollection($categoriesList, $category->subcategories); 
    } 
} 
var_dump($categoriesCollection); 

輸出:

Array 
(
    [0] => stdClass Object 
     (
      [name] => IT 
      [subcategories] => Array 
       (
        [0] => stdClass Object 
         (
          [name] => Internet 
          [subcategories] => Array 
           (
            [0] => stdClass Object 
             (
              [name] => Web Development 
             ) 

            [1] => stdClass Object 
             (
              [name] => Browsers 
             ) 

           ) 

         ) 

       ) 

     ) 

) 
+0

非常感謝您對前面回答。對於遲到的回覆感到抱歉,但那正是我期待的 – aafonso1991

1

試試這個,請(未測試,也許有些錯誤糾正正確的方向,我在這裏幫助):

function boolean check_is_good($array, $input) 
{ 
$element = array_shift($input); 
for ($x = 0; $x < count($array), $x++) { 
     if ($array[$x]['name'] == $element){ 
      if ((!isset($array[$x]['subcategories']) && (count($input) == 0)) 
       return (true); 
      else if ((!isset($array[$x]['subcategories']) && (count($input) != 0)) 
       return (false); 
      $newArray = $array[$x]['subcategories']; 
      return (check_is_good($newArray, $input)); 
     } 
    } 
    return (false); 
} 

函數返回TRUE,則一切都在正確的位置

你在參數1代表您的JSON($ category_collecton我一個數組來傳遞n您的例子)

你必須在參數2通過在你的榜樣與所有元素的數組($項目)