从数组中获取所有值

时间:2018-05-18 08:43:46

标签: php arrays

我不知道如何从所有数组中获取“name”值?

任何人请帮帮我

我有一些像这样的数组

Array
    (
        [0] => Array
            (
                [name] => Jon
                [phone] => 
                [relation] => wife
                [age] => 43
                [relative_education] => 4
            )

        [1] => Array
            (
                [name] => John
                [phone] => 123456789
                [relation] => son
                [age] => 24
                [relative_education] => 10
            )

        [2] => Array
            (
                [name] => Amy
                [phone] => 456789123
                [relation] => Son
                [age] => 21
                [relative_education] => 12
            )        
    )

提前致谢。

3 个答案:

答案 0 :(得分:4)

试试这个

$name =  array_column($data, 'name');
print_r($name);

答案 1 :(得分:1)

$names = array_map(function($user) {
    return $user['name'];
}, $users);

答案 2 :(得分:1)

您可以循环数组以获取值。 如果你的数组是$ arr,那么使用下面的代码来获取值

//$arr = YOUR ARRAY
$names = array();
foreach($arr as $val) {
        $names[] = $val['name'];
 }

print_r($names);