1

I am trying to make an array in this type of format

Array
(
    [name] => 'Nick'
    [email] => '[email protected]'
    [groups] => Array (
        [0] => 'group1'
        [1] => 'group2'
        [2] => 'group3'
    )
)

name and email will contain one value. groups will contain multiple values (different for all people).

So I am in a loop defined like so

foreach($result as $info) {

}

Now the $info variable contains a lot of data, about lots of users. To get the name and email of each user, I can do

$name = $info["name"][0];
$email = $info["email"][0];

I can then start building the array I am attempting to make

$userData = array(
    "name" => $name,
    "email" => $email
);

To get the groups, I need to do

foreach($info["group"] as $group) {
    print_r($group["name"]);    
}

My code at the moment looks like the following $userData = array();

foreach($result as $info) {

    $name = $info["disName"][0];
    $email = $info["email"][0];

    $userData = array(
        "name" => $name,
        "email" => $email
    );

    foreach($info["group"] as $group) {
        print_r($group["name"]);
    }

}

How can I get this data I am extracting into an Array in the format I need?

Thanks

2
  • Well what's the output you are getting with the current code? Commented Sep 30, 2015 at 11:22
  • $userData = array("name" => $name, "email" => $email, "groups" => $info["group"]["name"]); won't do the trick? Commented Sep 30, 2015 at 11:38

1 Answer 1

1

Modify your code..

foreach($result as $info) {

    $name = $info["disName"][0];
    $email = $info["email"][0];

    $userData = array(
        "name" => $name,
        "email" => $email
    );

    foreach($info["group"] as $group) {
        $groups[] = $group;
    }

    $userData['group'] = $groups;

}

$groups is an array for index $userData['group']

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

1 Comment

What's the difference between your last foreach and simply $userData['group'] = $info['group'] ?

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.