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

时间:2013-05-14 08:33:34

标签: php

我知道密钥,现在我需要所有结果,当我搜索5000+用户数据库时会产生这些结果。任何用户都可能没有,一个或多个位置,由idname字段标识。因此,我需要一个数组中的结果,而不仅仅是第一个/最后一个,而是所有数组。下面是一个用户示例(实际数组设置,假数据;))

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

正如你所知,这家伙在他的脸书数据中有1个位置,但其他位置包含相同的[location] => Array ( [id] => 123 [name] => SomePlace ),例如[work][education],或者,或者......

我尝试过像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' );

如何修改上述函数以返回位置数组数组?这个返回包含位置的第一个用户的上述数据,而不是位置本身或它们的数组。

修改 我需要的是这样的事情:

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

根据回答进行修改 Thomas David Plat的回答给了我正确的结果,但仍然是一个不太有用的数据结构。如何从结构中得到结果并进入上面的编辑?

David回答的结果:

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

我尝试过array_filterarray_maparray_values以及其他类似的变体:

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

但结构似乎仍然存在......

2 个答案:

答案 0 :(得分:1)

更新的答案     

$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);
?>

这样可行,但正如我在评论中所说,这是一个不好的做法,因为它使用了全局关键字。如果将此函数作为方法实现到类中,而不是使用global关键字,则可以使用类变量,这将是一个简洁的解决方案。

旧答案

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>";

你在这里。只需将一组用户传递给该函数即可。该函数将返回一组位置数组。

有一个限制。该功能不会搜索位置内的位置。

答案 1 :(得分:0)

您可以像这样修改数组的位置部分

[location] => Array
        (
            [work]=> Array(
                 [id] => 108276092536515
                  [name] => Arnhem, Netherlands
             )
             [home] => Array(
                 [id] => 108276092536515
                  [name] => Arnhem, Somewhere else
             )
        )

如果您像这样修改数组,那么您的函数将返回位置数组

相关问题