Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I have a array list of staff in single array and I am trying to make that multi-dimension based on line manager. So in my example below Miriam Wood manages Alan Haworth and Guy Kahane, and Guy Kahane manages Matthew Baum and Rebecca Roache based on Line Manger field. So my current array is looking some like this.

Looking to do some from

  1. Miriam Wood - no line manager
  2. Alan Hanworth - line manager -> id 1
  3. Guy Kahane - line manager -> id 1
  4. Matthew Baum - line manager -> id 3
  5. Rebecca Roache - line manager -> id 3

to

  • Miriam Wood
    • Alan Hanworth
    • Guy Kahane
      • Matthew Baum
      • Rebecca Roache


[98] => Array ([Id] => 1, [Name] => Miriam Wood, [Line Manager] => None)

[99] => Array([Id] => 2, [Name] => Alan Haworth, [Line Manager] => 1)

[105] => Array([Id] => 3, [Name] => Guy Kahane, [Line Manager] => 1)

[106] => Array([Id] => 4, [Name] => Matthew Baum, [Line Manager] => 3)

[107] => Array([Id] => 5, [Name] => Rebecca Roache, [Line Manager] => 3)

And I would like to change it to

(
 [98] => Array
(
   [Id] => 1
   [Name] => Miriam Wood
   [Line Manager] => None
   [Staffs] => Array
(
    [99] => Array
    (
        [Id] => 2
        [Name] => Alan Haworth
        [Line Manager] => 1
    )

    [105] => Array
    (
        [Id] => 3
        [Name] => Guy Kahane
        [Line Manager] => 1
        [staffs] => Array
        (
            [106] => Array
            (
                [Id] => 4
                [Name] => Matthew Baum
                [Line Manager] => 3
            )

            [107] => Array
            (
                [Id] => 5
                [Name] => Rebecca Roache
                [Line Manager] => 3
            )
        )
    )
)

) )

share|improve this question
    
What have you tried so far? – AnotherGuy Jan 15 at 10:51
    
If this data comes from a database I would say there are more efficient ways to accomplish what you are trying to do with the SQL language the database is using. If that is the case try asking on Database Administrators (Stackexchange) – AnotherGuy Jan 15 at 11:26

2 Answers 2

this is simple example.

$head = array("id">1,"name"=>"Harshana","servents"=>array(
"s1"=>array("id">1,"name"=>"abc"),
"s2"=>array("id">2,"name"=>"sdf"),
"s3"=>array("id">3,"name"=>"fgh")
));
print_r($head);
echo "<hr />";
print_r($head["servents"]);
echo "<hr />";
print_r($head["servents"]["s1"]);
echo "<hr />";
$s1 = $head["servents"]["s1"];
echo "servent 1 name :".$s1["name"];

hope you can get idea for your matter.

share|improve this answer

I have attempted to solve your problem. I have made 3 functions to keep the responsibilities for each function down to a minimum. It looks like the following:

function build_employee_hierarchy(array $employees) {

    $groups    = build_employee_groups($employees);
    $hierarchy = build_employee_group_inheritance($employees, $groups);

    return $hierarchy[0];

}

function build_employee_group_inheritance(array $employees, array $groups) {

    $hierarchy = [];

    foreach($employees as $key => $employee) {

        $employee_id     = $employee['id'];
        $hierarchy[$key] = $employee;

        if(!array_key_exists($employee_id, $groups)) {
            continue;
        }

        $hierarchy[$key]['staff'] = build_employee_group_inheritance($groups[$employee_id], $groups);

    }

    return $hierarchy;
}

function build_employee_groups(array $employees) {

    $groups = [];

    foreach($employees as $employee) {

        if(array_key_exists('line-manager', $employee) && !is_null($employee['line-manager'])) {

            $groups[$employee['line-manager']][] = $employee;

        }

    }

    return $groups;

}

These functions first group all employees who belong to the same line manager. Then the inheritance of the employees is resolved recursively. The function build_employee_hierarchy serves as a utility method for building the whole hierarchy by providing a single array.

With the data you provided the result will look like the following. The only difference is that when there is no line manager it equals NULL instead of the string none.

array (size=4)
  'id' => int 1
  'name' => string 'Miriam Wood' (length=11)
  'line-manager' => null
  'staff' => 
    array (size=2)
      0 => 
        array (size=3)
          'id' => int 2
          'name' => string 'Alan Haworth' (length=12)
          'line-manager' => int 1
      1 => 
        array (size=4)
          'id' => int 3
          'name' => string 'Guy Kahane' (length=10)
          'line-manager' => int 1
          'staff' => 
            array (size=2)
              0 => 
                array (size=3)
                  'id' => int 4
                  'name' => string 'Matthew Baum' (length=12)
                  'line-manager' => int 3
              1 => 
                array (size=3)
                  'id' => int 5
                  'name' => string 'Rebecca Roache' (length=14)
                  'line-manager' => int 3

This may not be the most efficient solution, but because if uses recursion the array can be a long as you like.

OBS

If the data comes from a relational database there is most likely a much more efficient way to retrieve the data in the same form by using the SQL language the database uses. If that is the case ask the awesome people at Database Administrators (Stackexchange).

Hope this helps.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.