douguyi3903
2012-05-30 07:35 阅读 75
已采纳

在多维数组中查找值

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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

7条回答 默认 最新

  • 已采纳
    duanjian3920 duanjian3920 2012-05-30 07:45

    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.

    点赞 评论 复制链接分享
  • duanmiao6695 duanmiao6695 2012-05-30 07:38
    function getUsernamesByFavoriteGame($data, $game) {
        $usernames = array();
        foreach($data as $arr) {
            if ($arr['favoriteGame'] == $game) {
                $usernames[] = $arr['username'];
            }
        }
        return $usernames;
    }
    
    点赞 评论 复制链接分享
  • duanbamo0127 duanbamo0127 2012-05-30 07:38
     $usernames = array();
     foreach($array as $key => $value) {
         if ($value['favoriteGame'] == 'Mario') {
             $usernames[] = $value['username'];
         }
     }
    
    点赞 评论 复制链接分享
  • douyue1926 douyue1926 2012-05-30 07:44
     $game = 'Mario';   
     $users = array();
     foreach($array as $key => $value) {
         if ($value['favoriteGame'] == $game) {
             $users[] = $value['username'];
         }
     }
    
    点赞 评论 复制链接分享
  • dongtang7347 dongtang7347 2012-05-30 07:44

    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.

    点赞 评论 复制链接分享
  • douwu3763 douwu3763 2012-05-30 08:02

    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 
        ) 
    ) 
    
    点赞 评论 复制链接分享
  • dongyi9298 dongyi9298 2014-07-12 14:04
    $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
    )
    
    点赞 评论 复制链接分享

相关推荐