0

I have the following array:

$example = array(
array(
    'id' => 63,
    'parentid' => 0,
    'char_value' => 'End poverty in all its forms everywhere',
    'param_value' => 1,
    'children' => array(
        array(
            'id' => 84,
            'parentid' => 63,
            'char_value' => 'test 1',
            'param_value' => 1
        ),
        array(
            'id' => 85,
            'parentid' => 63,
            'char_value' => 'test 2',
            'param_value' => 1
        )
    )
));

And the following function for recursion:

function drawPropertyTree($array, $parent){
$result = array();
foreach ($array as $k => $v) {

    $pieces = explode(" ", $v['char_value'], 6);
    $name   = implode(" ", array_splice($pieces, 0, 3));

    $result[] = array(
        'id' => $v['id'],
        'parent' => $parent,
        'text' => $v['param_value'] . " " . $name
    );

    if (isset($v['children'])) {
        $result[] = drawPropertyTree($v['children'], $v['id']);
    }
}
return $result;}

I'd like to list all sub-arrays on the same level, with parent node. Here is the working sample and this is the current output:

Array
(
[0] => Array
    (
        [id] => 63
        [parent] => 0
        [text] => 1 End poverty in
    )

[1] => Array
    (
        [0] => Array
            (
                [id] => 84
                [parent] => 63
                [text] => 1 test 1
            )

        [1] => Array
            (
                [id] => 85
                [parent] => 63
                [text] => 1 test 2
            )

    )
)

This is desired result:

Array (
[0] => Array
    (
        [id] => 63
        [parent] => 0
        [text] => 1 End poverty in
    )
[1] => Array
        (
            [id] => 84
            [parent] => 63
            [text] => 1 test 1
        )

[2] => Array
        (
            [id] => 85
            [parent] => 63
            [text] => 1 test 2
        )

2 Answers 2

1

Just a slight change in your function required - given that you need support for arbitrary depth:

function drawPropertyTree($array, $parent)
{
    $result = array();
    foreach ($array as $k => $v) {

        $pieces = explode(" ", $v['char_value'], 6);
        $name   = implode(" ", array_splice($pieces, 0, 3));

        $result[] = array(
            'id'     => $v['id'],
            'parent' => $parent,
            'text'   => $v['param_value'] . " " . $name
        );

        if (isset($v['children'])) {
            // drawPropertyTree returns an array
            // which must be merged into the existing array
            $result = array_merge($result, drawPropertyTree($v['children'], $v['id']));
        }
    }
    return $result;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Doh... spent an hour on this... Thanks!
0

Make new empty array and push all elements in for loop, and check children to push them into new array as well. Without any recursion

2 Comments

Can you please provide any snippet for this?
Oh, I'm in train now. Try yourself for now, I may do something after a hour

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.