简体   繁体   English

从PHP数组访问数据

[英]Accessing data from PHP array

I have an array as follows 我有如下数组

$user_followers = [
    [
        'user_id'   => '1',
        'followers' => ['3', '4', '5']
    ],
    [
        'user_id'   => '2',
        'followers' => ['1', '5']
    ],
    [
        'user_id'   => '3',
        'followers' => ['1', '5', '4']
    ],
    [
        'user_id'   => '4',
        'followers' => ['3', '1', '5']
    ],
    [
        'user_id'   => '5',
        'followers' => ['1', '2', '4']
    ],
];

What I need to do is obtain the followers data array based on the user_id value therefore if I have a user_id of 2 it will return an array containing the followers array data. 我需要做的是基于user_id值获取followers数据数组,因此,如果我的user_id为2,它将返回一个包含followers数组数据的数组。 I am not sure whether I need to re organise my array structure to do this. 我不确定是否需要重新组织我的数组结构来做到这一点。

You can easily key the followers to all user_id s with array_column() , then you can access the followers by their user id: 您可以轻松地键followers所有user_id以s array_column()那么你可以通过自己的用户ID访问的追随者:

$followers = array_column($user_followers, 'followers', 'user_id');

print_r($followers[2]);

Which gives ( Demo ): 给出( 演示 ):

Array
(
    [0] => 1
    [1] => 5
)

This works as long as the key is unique (which is the third parameter of array_column and seems the case here as user_id sounds like a unique id). 只要键是唯一的, array_column (这是array_column的第三个参数,在这里似乎是这样,因为user_id听起来像是唯一的ID)。

If you version php < 5.5 you can't use array_column 如果您的PHP版本低于5.5,则不能使用array_column

Fatal error: Call to undefined function array_column()

So you can create your own function: 因此,您可以创建自己的函数:

if ( !function_exists('array_column') ) {
  function array_column( $collection, $field, $keyfield = null, $desired_id = null ) {
    $items = array();
    foreach ( $collection as $k => $item ) {
      $key = $keyfield ? $item[$keyfield] : $k;
        if( $desired_id == $key){     
            $items[] = $item[$field];
        }
    }
    return $items;
  }
}

var_dump(array_column($user_followers, 'followers', 'user_id', 2));

Result: 结果:

array (size=1)
  0 => 
    array (size=2)
      0 => string '1' (length=1)
      1 => string '5' (length=1)

Example - Demo 示例- 演示

Well, first of all, your array should look like this: 好吧,首先,您的数组应如下所示:

$users = array(
        array(
            'user_id'   => '1',
            'followers' => array('3', '4', '5')
        ),
        array (
            'user_id'   => '2',
            'followers' => array('1', '5')
        ),
        array(
            'user_id'   => '3',
            'followers' => array('1', '5', '4')
        ),
        array(
            'user_id'   => '4',
            'followers' => array('3', '1', '5')
        ),
        array(
            'user_id'   => '5',
            'followers' => array('1', '2', '4')
        ),
    );

You can create a function like this: 您可以创建如下函数:

// Returns the array of user data
function getUserFollowers($users, $desired_id) {
    foreach($users as $user) {
        if ( $user['user_id'] == $desired_id ) {
            return $user['followers'];
        }
    }
}

Where the parameter $users is the array of all your users and $id is the id of the user you are looking for. 其中,参数$ users是所有用户的数组,而$ id是要查找的用户的id。 The foreach loop iterates through all of your users and checks the id of all the users. foreach循环遍历所有用户并检查所有用户的ID。 If it matches, it returns the array of followers. 如果匹配,则返回关注者数组。

Note, this process can use up a lot of memory if the array is huge. 注意,如果数组很大,此过程可能会占用大量内存。 Where are you loading this data from? 您从何处加载此数据?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM