【问题标题】:Recursive Search Function递归搜索功能
【发布时间】:2014-05-05 18:25:39
【问题描述】:

我有以“IT/Internet/Web Development/Ajax”形式出现的字符串。我解析它并制作一个类似的 JSON 对象

[{
  "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/Internet/Browsers”,我希望能够解析创建的类别并将“Browsers”作为 Internet 的子类别放置在正确的位置,然后是我的 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...

        }

    }

如果有人能指导我正确的方向,将不胜感激

【问题讨论】:

    标签: php json recursion


    【解决方案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
                                            )
    
                                    )
    
                            )
    
                    )
    
            )
    
    )
    

    【讨论】:

    • 非常感谢您的回答。抱歉回复晚了,但这正是我想要的
    【解决方案2】:

    请试试这个(未经测试,可能有一些错误需要纠正,我是来帮忙的):

    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_colleton)

    您必须在参数 2 中传递一个包含所有元素的数组(在您的示例中为 $items)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-19
      • 2014-08-03
      • 2017-02-20
      • 1970-01-01
      • 2019-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多