0

I want to build and return an array in this format:

[
    [
        'prefLabel' => 'foo',
        'subclasses' => [
            [
                'prefLabel' => 'bar',
                'subclasses' => [
                    [
                        'prefLabel' => 'tee',
                        'subclasses' => [
                            [
                                //.. more of the same
                            ]
                        ]
                    ],
                    [
                        // ...
                    ]
                ]
            ],
            [
                'prefLabel' => 'baz',
                'subclasses' => [
                    [
                        // ...
                    ],
                    [
                        // ...
                    ]
                ]
            ],
        ]
    ],
]

The depth is unknown.

I realize this needs a recursive function, but the way I'm doing it always returns a single dimension, albeit with the correct subclasses:

public function search($keyword)
{
    $resp = $this->searchPrefLabels($keyword);

    foreach($resp->prefLabels as $obj) {
        $return[] = [
            'prefLabel' => $obj->prefLabel,
            'subclasses' => $this->getSubclasses($obj->prefLabel)
        ];
    }

    return $return;
}

// the recursive function
protected function getSubclasses($searchTerm, &$tree_string = []) 
{

    $resp = $this->searchSubClass($searchTerm);
    $tree = [];
    foreach($resp->subclasses as $subclass) {
        $tree[] = [
            'prefLabel' => $subclass->prefLabel
        ];
    }
    if(count($tree)) {
        $tree_string = array_merge($tree_string, $tree);
    }

    foreach($tree as $key => $val) {
        $val['subclasses'] = $this->getSubclasses($val['prefLabel'], $tree_string); 
    }

    return $tree_string;
}

The above returns something similar to the following:

[
    [
        'prefLabel' => 'foo',
        'subclasses' => [
            [
                'prefLabel' => 'bar',
            ],
            [
                'prefLabel' => 'baz',
            ],
            [
                'prefLabel' => 'tee',
            ],
            // ...
    ],
]

I want it to return in the nested format shown above.

1
  • @splash58 That did not change the returned format Commented Feb 21, 2018 at 20:44

1 Answer 1

1

I think you only need one function for this recursion (since top level looks like all sub levels):

public function getPreLabelsAndSubClasses($keyword) {
    $result = [];
    $labels = $this->searchPrefLabels($keyword);
    foreach ($labels as $label) {
        $result[] = [
            'prefLabel' => $label,
            'subclasses' => $this->getPreLabelsAndSubClasses($label),
        ];
    }
    return $result;
}
Sign up to request clarification or add additional context in comments.

1 Comment

There is a separate call to get subclasses though: $resp = $this->searchSubClass($searchTerm); How do I incorporate this in the solution you suggested?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.