0

I've a multidimensional array:

array (
    array (
        "username"        => "foo",
        "favoriteGame"    => "Mario"
    )
    array (
        "username"        => "bar",
        "favoriteGame"    => "Mario"
    )
    array (
        "username"        => "xyz",
        "favoriteGame"    => "Zelda"
    )
)

How could I get the usernames of the persons that like to play for example Mario the easiest way possible?

EDIT: My fault: forget to explicitly mention that the "favoriteGame" value is dynamic and I cannot know which it is in advance.

My Solution:

foreach($users as $key => $value)
{
    if(!isset($$value['favoriteGame']))
    {
        $$value['favoriteGame'] = array();
    }
    array_push($$value['favoriteGame'], $value['username']);
}

Iterate over each sub-array and find its favoriteGame value. If there is not already an array $favoriteGame create it. Push the username-value of the actual sub-array to the $favoriteGame array.

Thanks for your replies, I just couldn't phrase this question properly.

2
  • 1
    What do you mean by dynamic? How is it set dynamically? Commented May 30, 2012 at 7:42
  • This array is based on user-input. So it could be Mario, Zelda or even DeadSpace. I want to display a list with the values the players have entered alltogether (removing dublicates, of course). And foreach "favoriteGame" a list of the users that have entered this as "favoriteGame". Commented May 30, 2012 at 7:44

7 Answers 7

6
function getUsernamesByFavoriteGame($data, $game) {
    $usernames = array();
    foreach($data as $arr) {
        if ($arr['favoriteGame'] == $game) {
            $usernames[] = $arr['username'];
        }
    }
    return $usernames;
}
Sign up to request clarification or add additional context in comments.

Comments

5
 $usernames = array();
 foreach($array as $key => $value) {
     if ($value['favoriteGame'] == 'Mario') {
         $usernames[] = $value['username'];
     }
 }

Comments

4

I would use array_filter. If you have PHP 5.3 or up, you can do it like this:

$favorite = "Mario";
$filter = function($player) use($favorite) { return $player['favoriteGame'] == $favorite; };
$filtered = array_filter($players, $filter);

It will be a little different for older versions because you won't be able to use lambda functions.

1 Comment

+1 I find array_filter much easier to understand when dealing with multidimensional arrays
2
 $game = 'Mario';   
 $users = array();
 foreach($array as $key => $value) {
     if ($value['favoriteGame'] == $game) {
         $users[] = $value['username'];
     }
 }

Comments

2

If you are using this more often then convert the data structure to something like this.

 array(
   "Mario" => array(
               "0":"foo",
               "1":"xyz"
               )
   "Zelda" => array(
               "0":"pqr",
               "1":"abc"
              )
 )

This will directly give you list of user names for a favorite game.

$arr[$favGame]

If you cannot change the data structure then go with with tigrang has suggested.

Comments

1

I think you should implement a custom multidimensional search function.
Take a look at this answer.


Here's how you would use it

Code | Live example

function search($array, $key, $value){
    $results = array();

    if (is_array($array))
    {
        if (isset($array[$key]) && $array[$key] == $value)
            $results[] = $array;

        foreach ($array as $subarray)
            $results = array_merge($results, search($subarray, $key, $value));
    }

    return $results;
}

$arr = array (
    array (
        "username"        => "foo",
        "favoriteGame"    => "Mario"
    ),
    array (
        "username"        => "bar",
        "favoriteGame"    => "Mario"
    ),
    array (
        "username"        => "xyz",
        "favoriteGame"    => "Zelda"
    )
);

print_r(search($arr, 'favoriteGame', 'Mario'));

//OUTPUT
Array ( 
    [0] => Array ( 
        [username] => foo 
        [favoriteGame] => Mario 
    ) 
    [1] => Array ( 
        [username] => bar 
        [favoriteGame] => Mario 
    ) 
) 

Comments

0
$array = array( 'a' => 'A',
    'b'=>'B',
    'c'=>'C',
    'd'=>array(
        'e'=>array(
           'f'=>'D'
        ),
        'g'=>array(
            'h'=>'E'
        )
    ),
    'i'=>'F',
    'j'=>array(
        'k'=>'G'
    ),
    'l'=>'H'
);

$new_array = array();
foreach($array as $k1=>$v1){
    if(is_array($v1)){
        $new_array = parseArray($new_array, $k1, $v1);
    }else{
        $new_array = array_merge($new_array, array($k1=>$v1));
    }
}

function parseArray($new_array, $key, $val){
    if(is_array($val)){
        foreach($val as $k2=>$v2){
            if(is_array($v2)){
               $new_array = parseArray($new_array, $k2, $v2);
            }else{
               $new_array = array_merge($new_array, array($k2=>$v2));
            }
        }
    }else{
        $new_array = array_merge($new_array, array($key=>$val));
    }
    return $new_array;
}

Output

Array
(
    [a] => A
    [b] => B
    [c] => C
    [f] => D
    [h] => E
    [i] => F
    [k] => G
    [l] => H
)

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.