1

having an issue with something. I have an array called result which is in the following format:

Array
(
    [count] => 74
    [0] => Array 
    (
        [15] => usncreated
        [memberof] => Array
        (
            [count] => 3
            [0] => GroupA
            [1] => GroupB
            [2] => GroupC
        )

    )
    [1] => Array 
    (
        [15] => usncreated
        [memberof] => Array
        (
            [count] => 2
            [0] => GroupA
            [1] => GroupF
        )

    )
)

I am trying to build up my own array now. At the moment I have

foreach($result as $user) {

    if(isset($user["memberof"])){
        foreach($user["memberof"] as $group) {
            $groups[] = $group;
        }

     }
     $userData["group"] = $groups;

     print_r("<pre>");
        print_r($userData);
     print_r("</pre>");
 }

Problem is my output is like so

Array
(
    [group] => Array
    (
        [0] => 2
        [1] => GroupA
        [2] => GroupB
    )
)

Array
(
    [group] => Array
    (
        [0] => 2
        [1] => GroupA
        [2] => GroupB
        [3] => 2
        [4] => GroupA
        [5] => GroupF
    )
)

So the second one repeats the first. If I move the print_r somewhere else, like outside the loop, I just see one user with every possible group. I know it needs to print in a loop, but whatever I try I cant get the correct output.

How can I get it to only display the groups for each user?

Thanks

1
  • foreach($user["memberof"] as $group) is useless as your requirment Commented Sep 30, 2015 at 13:18

1 Answer 1

1
foreach($result as $user) {

    $groups = array(); // RESET GROUPS ARRAY

    if(isset($user["memberof"])){
        foreach($user["memberof"] as $group) {
            $groups[] = $group;
        }

     }
     $userData["group"] = $groups;

     print_r("<pre>");
        print_r($userData);
     print_r("</pre>");
 }

Reset the groups array for each user found in results.

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.