A.L.
A.L.

Reputation: 173

PHP recursive function for concatenating a string

I'm trying to make a CSV file like this:

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

This is basically a recursion where we have parents and children. So far I have them stored in database and I can extract them as a two-dimensional array. The problem comes when I try to make the string for the rows in the CSV file. So far, I have this:

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;
        }

The result is the array shown below:

[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
        )
.......

I don't have a problem with the making of the csv file, but with the nested concatenation of the string. The string will build a D3 tree diagram and this is the preferred formatting as I don't have control over that. I'm missing something but at this point, I'm not sure what.

Upvotes: 1

Views: 776

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57131

This is difficult to test, but I think the problem is that you are adding to $str all the time. So each time round the loop, you will add the next level string onto it, eventually you reset it.

Rather than do that, this takes $str and adds the extra part on, assigning this to a new string and uses this value, including passing it on to the next level...

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;
}

As I can't test this, it's impossible to check.

Upvotes: 2

Related Questions