1

I'm working in PHP and I have an multidimensional array which contains data about user groups, the array is current set out as follows.

$groups = array(
               10 => array("name" => "Admin", (etc)), 
                5 => array("name" => "Standard", (etc))
          );

The user will have a group value, which will either be "Admin" or "Standard" but as these values are not the key value in the array, I don't know how I will be able to search for the string value within the child array.

I'm able to change to an integer based level system, but I would prefer to do it this way.

So my question is, how am I able to search a multidimensional array for a value of a subject array without knowing it's key value?

2 Answers 2

2

Try this one:

$admins = array_filter($groups, function($data) {
    return $data['name'] == 'Admin';
});
Sign up to request clarification or add additional context in comments.

Comments

0

Browse it :)

$admins = array();
$standards = array();
foreach($groups AS $group)
{
    // Search for administrators in the 1-dimension array.
    $admins = array_merge($admins, array_keys($group, "Admin"));

    // Search for standard users in the 1-dimension array.
    $standards = array_merge($standards, array_keys($group, "Standard"));
}

You can use functions such as array_search or array_keys once you work with 1-dimension arrays. You'll find more information about the functions above in the PHP Manual.

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.