0

Trying to create an organizational chart, or nested array, where we have one final array with each person under their supervisor. I am imagining some kind of array walk where a function is called when a condition is met - but unsure how to keep calling the same function when going several relationships deep. Final array should look like:

Array
(
    [0] => 1
    [1] => Array
        (
            [0] => 2
            [1] => Array
                (
                    [0] => 3
                    [1] => 4
                    [2] => 5
                    [3] => Array
                        (
                            [0] => 6
                        )

                )

        )

)
personnel_id personnel_firstname personnel_supervisor
1 jeff -
2 todd 1
3 maria 2
4 john 2
5 sarah 2
6 joe 5
Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
Jon Erickson
  • 1,876
  • 4
  • 30
  • 73
  • 1
    Does this answer your question? [Recursive function to generate multidimensional array from database result](https://stackoverflow.com/questions/8587341/recursive-function-to-generate-multidimensional-array-from-database-result) – Definitely not Rafal Apr 26 '21 at 17:13
  • @DefinitelynotRafal yes, thank you. Couldn't figure how to search for it, the tree term evaded my ideas. – Jon Erickson Apr 26 '21 at 17:30

1 Answers1

0

First of all, you might consider reshaping your array to be associative so that any given person's subordinates are directly associated with them. Consider how I structured the 'original data' array below.

Doing it like this also translates well to JSON as a human-readable object

<?php

// original data

$chart = array(
    array(
        "name" => "John",
        "id" => 1,
        "staff" => array(
            array("name" => "Jane", "id" => 2),
            array(
                "name" => "Sally", "id" => 3,
                "staff" => array(
                    array("name" => "Bill", "id" => 4),
                    array("name" => "Fred", "id" => 5)
                )
            ),
            array("name" => "Alan", "id" => 6)
        )
    ),
    array("name" => "Susan", "id" => 7)
);

function extractStaff($array)
{
    // flatten the array out so we can iterate our table
    $tableArray = array();
    foreach ($array as $person) {
        $tableArray = getChildren($person, $person['name'], $tableArray);
    }
    return $tableArray;
}

function getChildren($person, $supervisor, $tableArray)
{
    //first add in the top level person
    $supe =  ($person['name'] == $supervisor) ? "-" : $supervisor;
    $tableArray[] = array("id" => $person['id'], "name" => $person['name'], "supervisor" => $supe);
    if ($person['staff'] && count($person['staff']) > 0) {
        foreach ($person['staff'] as $staff) {
            $tableArray = getChildren($staff, $person['name'], $tableArray);
        }
    }
    return $tableArray;
}

$tableArray = extractStaff($chart);
?>

Then you can just iterate through your array

<table>
<?php foreach ($tableArray as $person) {?>
<tr>
<td><?=$person['id']?> </td>
<td><?=$person['name']?> </td>
<td><?=$person['supervisor']?> </td>
</tr>
<?php }?>
</table>
Kinglish
  • 23,358
  • 3
  • 22
  • 43