0

I'm creating a multi-dimensional array to match the hierarchy of a company. I ask the database for the downline (saved in a seperate table) and start looping it. Currently I'm only doing this for the highest level and the people below him. I know he has 6 people below him, thus the end result only shows one. When I var_dump $complete every time in the loop, I see different values in the children-array. (AKA he overwrites instead of adds up).

I've tried both array_push and this method, I hope I've been clear enough, because I probably don't (sorry.)

The code:

foreach ($downpositions as $downposition) {    
        // start position
        if ($downposition['PositionsDownposition']['positionsdownpositions_rel_position'] == 1) {
            // check downline only on first level
            if ($downposition['PositionsDownposition']['positionsdownpositions_level'] == 1) {

                foreach ($downpositions as $mother) {

                    if ($mother['PositionsDownposition']['positionsdownpositions_rel_downposition'] == 1) {

                        // format it
                        $node = array(
                            'id' => $downposition['PositionsDownposition']['positionsdownpositions_id'],
                            'name' => $downposition['PositionsDownposition']['positionsdownpositions_rel_position'],
                            'children' => array()
                        );

                        $complete = array(
                            'id' => $mother['PositionsDownposition']['positionsdownpositions_id'],
                            'name' => $mother['PositionsDownposition']['positionsdownpositions_rel_position'],
                            'children' => array()
                        );

                        // add up to array
                        $complete['children'][] = $node;
                    }
                }
            }
        }
    }
0

1 Answer 1

1

Found the problem, im intialising $complete['children'] in the loop, thus resetting the array to empty all the time.

Solution:
Re-do the code, initialise of $complete outside the loop.

Sign up to request clarification or add additional context in comments.

Comments

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.