3

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

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

如您所见,此人在他的 Facebook 数据中有 1 个位置,但其他位置包括相同[location] => Array ( [id] => 123 [name] => SomePlace )的位置,例如[work]or [education]、 or 、 or 或...

我试过这样的递归函数

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 的答案进行编辑 答案 给了我正确的结果,但仍然不是一个非常可用的数据结构。如何从结构中获取结果并进入上述编辑中显示的结果?

大卫回答的结果:

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_filter,如下所示:array_maparray_values

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

但结构似乎保持...

4

2 回答 2

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 关键字。如果将此函数作为方法实现到类中,则可以使用类变量而不是使用 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>";

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

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

于 2013-05-14T08:59:43.380 回答
0

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

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

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

于 2013-05-14T08:55:42.053 回答