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.

iceteea
  • 1,214
  • 3
  • 20
  • 35
  • 1
    What do you mean by dynamic? How is it set dynamically? – tigrang May 30 '12 at 07: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". – iceteea May 30 '12 at 07:44

7 Answers7

6
function getUsernamesByFavoriteGame($data, $game) {
    $usernames = array();
    foreach($data as $arr) {
        if ($arr['favoriteGame'] == $game) {
            $usernames[] = $arr['username'];
        }
    }
    return $usernames;
}
tigrang
  • 6,767
  • 1
  • 20
  • 22
5
 $usernames = array();
 foreach($array as $key => $value) {
     if ($value['favoriteGame'] == 'Mario') {
         $usernames[] = $value['username'];
     }
 }
Aurimas Ličkus
  • 9,886
  • 4
  • 24
  • 26
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.

Okonomiyaki3000
  • 3,628
  • 23
  • 23
2
 $game = 'Mario';   
 $users = array();
 foreach($array as $key => $value) {
     if ($value['favoriteGame'] == $game) {
         $users[] = $value['username'];
     }
 }
Ruben Nagoga
  • 2,178
  • 1
  • 19
  • 30
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.

Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47
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 
    ) 
) 
Community
  • 1
  • 1
ShadowScripter
  • 7,314
  • 4
  • 36
  • 54
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
)
MH2K9
  • 11,951
  • 7
  • 32
  • 49