使用字符串递归搜索数组中的子字符串

时间:2015-07-09 15:41:12

标签: php arrays string recursion

对于下一个数组,每个子数组的键表示node-id(0)和类别名称(1)。
我想要做的是找到每个子类别的父节点id
例如,' 3968976031'的父ID是' 10176091'以及' 3968980031'的父母ID是' 3938977031' 为了定义谁是父母,我们必须查看类别:
Küche & Haushalt/Heimtextilien, Bad- & Bettwaren/Babybettausstattung的父级是Küche & Haushalt/Heimtextilien, Bad- & Bettwaren

任何形式的帮助将不胜感激。


    array(6) {
      [0]=>
      array(2) {
        [0]=>
        string(8) "10176091"
        [1]=>
        string(48) "Küche & Haushalt/Heimtextilien, Bad- & Bettwaren"
      }
      [1]=>
      array(2) {
        [0]=>
        string(10) "3968976031"
        [1]=>
        string(68) "Küche & Haushalt/Heimtextilien, Bad- & Bettwaren/Babybettausstattung"
      }
      [2]=>
      array(2) {
        [0]=>
        string(10) "3968977031"
        [1]=>
        string(88) "Küche & Haushalt/Heimtextilien, Bad- & Bettwaren/Babybettausstattung/Babybettausstattung"
      }
      [3]=>
      array(2) {
        [0]=>
        string(10) "3968978031"
        [1]=>
        string(99) "Küche & Haushalt/Heimtextilien, Bad- & Bettwaren/Babybettausstattung/Babybettausstattung/Bettbezüge"
      }
      [4]=>
      array(2) {
        [0]=>
        string(10) "3968979031"
        [1]=>
        string(99) "Küche & Haushalt/Heimtextilien, Bad- & Bettwaren/Babybettausstattung/Babybettausstattung/Bettdecken"
      }
      [5]=>
      array(2) {
        [0]=>
        string(10) "3968980031"
        [1]=>
        string(104) "Küche & Haushalt/Heimtextilien, Bad- & Bettwaren/Babybettausstattung/Babybettausstattung/Bettwäsche-Sets"
      }
    }

2 个答案:

答案 0 :(得分:0)

使用foreach:

$seenCategories = array();
foreach($myArray as $key => $subArray) {

    $id = $subArray[0];
    $category = $subArray[1]; 
    // $category = Küche & Haushalt/Heimtextilien, Bad- & Bettwaren/Babybettausstattung/Babybettausstattung/Bettwäsche-Sets

    // saving category in order to be found as a parent in the future
    $seenCategories[$category] = $id;

    // parsing category in order to find parent
    $categoriesList = explode('/', $category);
    $nbSubCategories = count($categoriesList);
    // we remove the last subcategory (Bettwäsche-Sets) to find the parent
    unset($categoriesList[$nbSubCategories-1]);
    $parentCategory = implode("/", $categoriesList);
    // $parentCategory = Küche & Haushalt/Heimtextilien, Bad- & Bettwaren/Babybettausstattung/Babybettausstattung

    // deleted loop due to performence leak
    /*$parentId = null;
    for($testingKey = $key-1; $testingKey >= 0; $testingKey--) {
        if($parentCategory == $myArray[$testingKey][1]) {
            // we found the parent
            $parentId = $myArray[$testingKey][0];
            // let's exit loop
            break;
        }
    }*/

    // performence improvement
    // if we've already seen the parent, we get its id (we suppose a parent can't be after a children)
    if(array_key_exists($parentCategory, $seenCategories) {
        $parentId = $seenCategories[$parentCategory];
    } else {
        $parentId = null;
    }

    echo "id of item : $id, category : $category, parentId : $parentId";
}

编辑:

根据Mr.DOS的评论改善了表现。感谢。

答案 1 :(得分:0)

如果子节点始终跟在父节点上,并且数组只包含树的一个分支,我们可以很容易地根据数组中的前一个子类获取每个子类别的父ID。这种方法比@ Random的解决方案效率更高,因为它不需要为每个子数组在整个数组上进行第二次传递,并且它不涉及字符串操作。

//dt[text()="3TG:"]/following-sibling::dd

<dt>3TG:</dt> 
<dd>Does not contain 3TG</dd>
相关问题