dongyan5641 2013-05-14 08:33
浏览 52
已采纳

使用键从多维数组中获取值(复数)

I know the key, now I need ALL of the results that would yield when I search the 5000+ user database. Any user may have none, one or multiple locations, identified by an id and name field. Therefore I need the results in an array, not just the first/last one, but all of them. Below is an example of a user (actual array setup, fake data ;) )

Array
(
[ID] => 2
[user_login] => SomeGuy
[user_pass] => GreatEncryptedPassword
[user_nicename] => Some Guy
[user_email] => someguy@has-email.com
[user_url] => someguy.com
[user_registered] => 2013-04-11 11:18:58
[user_activation_key] => 
[user_status] => 0
[display_name] => Some Guy
[umeta_id] => 31
[user_id] => 2
[meta_key] => facebookmeta
[meta_value] => Array
    (
        [id] => 1234567890
        [name] => Some Guy
        [first_name] => Some
        [last_name] => Guy
        [link] => http://www.facebook.com/someguy
        [username] => someguy
        [birthday] => 02/21/1983
        [location] => Array
            (
                [id] => 108276092536515   //actual ID, try at facebook.com/108276092536515
                [name] => Arnhem, Netherlands
            )

        [gender] => male
        [relationship_status] => In a Relationship
        [significant_other] => Array
            (
                [name] => Some Chick
                [id] => 12345678906789
            )

        [email] => someguy@has-email.com
        [timezone] => 2
        [locale] => nl_NL
        [verified] => 1
        [updated_time] => 2013-04-02T09:28:30+0000
    )
)

As you can make out, this guy has 1 location in his facebook data, but other locations include the same [location] => Array ( [id] => 123 [name] => SomePlace ) for example for [work] or [education], or, or, or...

I've tried a recursive function like this:

function get_value_by_key( $array, $key ){
    foreach( $array as $k => $each ){
        if( $k == $key ){
            return $each;
        }

        if( is_array( $each )){
            if( $return = get_value_by_key($each,$key)){
                return $return;
            }
        }
    }
}

print_r( get_value_by_key( $users, 'location' );

How can I modify the above function to return an array of location arrays? This one return the above data of the first user containing a location, not the location itself or an array of them.

EDIT: What I need as a result is something like this:

Array(
    [0] => Array(
         [id] => 1234567890
         [name] => GreatCity, World
        )
    [1] => Array(
         [id] => 2345678901
         [name] => SomeCity, Blop
        )
    [2] => Array(
         [id] => 3456789012
         [name] => AnotherCity, Boo
        )
)

Edit based on answer Answer from Thomas David Plat gives me the correct results, but still a not very usable data structure. How to get the results out of the structure and into that shown in the above edit?

Results from David's answer:

Array
(
[0] => Array
    (
        [0] => Array
            (
                [0] => Array
                    (
                        [id] => 108276092536515
                        [name] => Arnhem, Netherlands
                    )
                [1] => Array()
            )
    )
[1] => Array
    (
        [0] => Array
            (
                [0] => Array
                    (
                        [id] => 108276092536515
                        [name] => Arnhem, Netherlands
                    )
                [1] => Array()
            )
    )
[2] => Array()

[3] => Array()

[4] => Array()

I've tried variations of array_filter, array_map, array_values and others like below:

 $locations = array_map( 'array_filter', find_array_values( $users, 'location' ));

But the structure seems to stay...

  • 写回答

2条回答 默认 最新

  • doupingzhi9674 2013-05-14 08:59
    关注

    UPDATED ANSWER

    $matches = array();
    
    function find_array_values($haystack, $needle)
    {
    
        global $matches;
        $collection = array();
    
        foreach($haystack AS $key => $value)
        {
            if($key === $needle)
            {
                $matches[] = $value;
            }
            else if(is_array($value))
            {
               find_array_values($value, $needle);
            }
        }
    }
    
    find_array_values($array, 'location');
    print_r($matches);
    ?>
    

    This will work, but as I already said in the comments it's a bad practice since it uses the global keyword. If you implement this function as a method into a class, instead of using the global keyword you could use a class variable which would be a neat solution.

    OLD Answer

    function find_array_values($haystack, $needle)
    {
        $collection = array();
    
        foreach($haystack AS $key => $value)
        {
            if($key === $needle)
            {
                $collection[] = $value;
            }
            else if(is_array($value))
            {
               $collection[] = find_array_values($value, $needle);
            }
        }
    
        return $collection;
    
    }
    
    echo "<pre>";
    print_r(find_array_values($users, 'location'));
    echo "</pre>";
    

    Here you are. Just pass an array of your users into the function. The function will return an array of arrays of locations.

    There's one restriction. The function will not search trough locations that are within locations.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)