所有数组成员的php字段[重复]

时间:2023-02-09 21:37:10

This question already has an answer here:

这个问题在这里已有答案:

What I'm looking for is simple but I have no idea about what to search on google.

我正在寻找的很简单,但我不知道在谷歌上搜索什么。

I look for a simple PHP command to give me what I want without using loop:

我寻找一个简单的PHP命令,在不使用循环的情况下给我我想要的东西:

$myarray= [
     ['id'=>5,'username'=>'aaa','password'=>'.....'],
     ['id'=>6,'username'=>'bbb','password'=>'.....'],
     ['id'=>7,'username'=>'ccc','password'=>'.....'],
     ['id'=>8,'username'=>'ddd','password'=>'.....'],
];

What I want to obtain:

我想要获得的:

array('aaa','bbb','ccc','ddd')

Any simple way?

任何简单的方法?

2 个解决方案

#1


1  

Use array_map (PHP 4 >= 4.0.6, PHP 5):

使用array_map(PHP 4> = 4.0.6,PHP 5):

function get_usernames($a) {
    return $a['username'];
}

$values = array_map('get_usernames', $myarray);

#2


8  

Have a look at array_column (this is available in >= PHP 5.5 [thanks for the tip kalley])

看看array_column(这在> = PHP 5.5中可用[感谢提示kalley])

$myarray= [
    ['id'=>5,'username'=>'aaa','password'=>'.....'],
    ['id'=>6,'username'=>'bbb','password'=>'.....'],
    ['id'=>7,'username'=>'ccc','password'=>'.....'],
    ['id'=>8,'username'=>'ddd','password'=>'.....'],
];
$values = array_column($myarray, "username");

#1


1  

Use array_map (PHP 4 >= 4.0.6, PHP 5):

使用array_map(PHP 4> = 4.0.6,PHP 5):

function get_usernames($a) {
    return $a['username'];
}

$values = array_map('get_usernames', $myarray);

#2


8  

Have a look at array_column (this is available in >= PHP 5.5 [thanks for the tip kalley])

看看array_column(这在> = PHP 5.5中可用[感谢提示kalley])

$myarray= [
    ['id'=>5,'username'=>'aaa','password'=>'.....'],
    ['id'=>6,'username'=>'bbb','password'=>'.....'],
    ['id'=>7,'username'=>'ccc','password'=>'.....'],
    ['id'=>8,'username'=>'ddd','password'=>'.....'],
];
$values = array_column($myarray, "username");