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条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路