PHP 用于连接字符串的递归函数

PHP recursive function for concatenating a string

我正在尝试制作这样的 CSV 文件:

id,value
1,type
2,type.type A
3,type.type A.subtype 2000
4,type.type A.subtype 2000.subsubtype
5,type.type A.subtype 2000.subsubtype.subsubsubtype
6,type.type B

这基本上是一个递归,我们有 parents 和 children。到目前为止,我将它们存储在数据库中,我可以将它们提取为 two-dimensional 数组。当我尝试为 CSV 文件中的行创建字符串时,问题就来了。 到目前为止,我有这个:

private function _criteriaRec(&$result, $parentId, &$str) {

            $relations = parent::getAll('objects_relations', 'id, object_id_2', ['object_id', $parentId, 'attr', 'criteria']); // here I'm extracting the objects from DB
            if(!empty($relations)){
                foreach ($relations as $relation) {
                    $object = parent::get('objects', 'id, title', '', $relation['object_id_2']);
                    $str .= ".$object[title]"; // I'm trying to make the string
                    $result[] = array(
                        'parent' => $parentId,
                        'child' => $relation['object_id_2'],
                        'title' => $object['title'],
                        'str' => $str
                    );
                    $this->_criteriaRec($result, $relation['object_id_2'], $str);
                    $str = ''; // I'm clearing the string at some point and I've moved this almost everywhere
                }
            }

            return $result;
        }

结果是如下所示的数组:

[0] => Array
        (
            [parent] => 17
            [child] => 33
            [title] => type
            [str] => .type
        )

    [1] => Array
        (
            [parent] => 33
            [child] => 34
            [title] => subtype B
            [str] => .type.subtype B
        )

    [2] => Array
        (
            [parent] => 33
            [child] => 35
            [title] => subtype A
            [str] => .subtype A
        )

    [3] => Array
        (
            [parent] => 35
            [child] => 36
            [title] => subsubtype
            [str] => .subtype A.subsubtype
        )
.......

我对 csv 文件的制作没有问题,但对字符串的嵌套连接没有问题。该字符串将构建一个 D3 树图,这是首选格式,因为我无法控制它。我遗漏了一些东西,但此时我不确定是什么。

这很难测试,但我认为问题在于您一直在向 $str 添加内容。所以每次循环,你都会在上面添加下一个级别的字符串,最终你会重置它。

而不是那样做,这需要 $str 并添加额外的部分,将其分配给一个新字符串并使用该值,包括将其传递到下一个级别...

private function _criteriaRec(&$result, $parentId, $str) {

    $relations = parent::getAll('objects_relations', 'id, object_id_2', ['object_id', $parentId, 'attr', 'criteria']); // here I'm extracting the objects from DB
    if(!empty($relations)){
        foreach ($relations as $relation) {
            $object = parent::get('objects', 'id, title', '', $relation['object_id_2']);
            $newStr = "$str.$object[title]"; 
            $result[] = array(
                        'parent' => $parentId,
                        'child' => $relation['object_id_2'],
                        'title' => $object['title'],
                        'str' => $newStr
            );
            $this->_criteriaRec($result, $relation['object_id_2'], $newStr);
        }
    }

    return $result;
}

因为我无法测试这个,所以无法检查。