简体   繁体   English

如何根据另一个数组键的值创建数组?

[英]How can I create an array from the values of another array's key?

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

$arr1 = array(
  0 => array(
    'name' => 'tom',
    'age' => 22
  ),
  1 => array(
    'name' => 'nick',
    'age' => 18
  )
);

However I want to create an array from it which consists of all the names, so it would become: 但是我想从它创建一个由所有名称组成的数组,因此它将成为:

$arr2 = array('tom', 'nick');

I have looked at array_filter() , but that would not work as this is a multi-dimensional array! 我看过array_filter() ,但这不会起作用,因为这是一个多维数组!

Question

How can I create an array with the values of a specific key ( name ) from another multi-dimensional array? 如何使用来自另一个多维数组的特定键( name )的值创建数组?

Newer versions of PHP allow using array_map() with a function expression instead of a function name: 较新版本的PHP允许使用带有函数表达式而不是函数名的array_map()

$arr2 = array_map(function($person) {
    return $person['name'];
}, $arr1);

But if you are using a PHP < 5.3, it is much easier to use a simple loop, since array_map() would require to define a (probably global) function for this simple operation. 但是如果使用PHP <5.3,则使用简单循环要容易得多,因为array_map()需要为这个简单的操作定义一个(可能是全局的)函数。

$arr2 = array();

foreach ($arr1 as $person) {
    $arr2[] = $person['name'];
}

// $arr2 now contains all names

This can be done in still more simple way by using array_column 这可以通过使用array_column以更简单的方式完成

$arr2= array_column($arr1, 'name');

print_r($arr2); //Array ( [0] => tom [1] => nick )

array_column is used to get the columns of a sub-array. array_column用于获取子数组的列。

$array = array(0 => array('name' => 'tom', 'age' => 22), 1 => array('name' => 'nick', 'age' => 18));
foreach($array as $arr => $a){
    $names[] = $array[$arr]["name"];
}

print_r($names); //Array ( [0] => tom [1] => nick ) 

如果您使用的是Laravel,那么只需使用array_pluck

$arr2 = array_pluck($arr1 , 'name');

暂无
暂无

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

相关问题 如何从PHP中具有相同键的另一个数组向数组的键添加值? - How can I add a value to an array's key from another array with the same key in PHP? 如何将与数组值关联的键保存到另一个数组? - How can I save an array values associated key to another array? 如何从另一个数组的值创建一个数组 - How to create an array from values of another array 如何将数组中的值附加到另一个数组中的另一个键 - How to attach values from an array to another key in another array 仅从另一个数组的值创建关联数组 - PHP - Create an associative array from another array's values only - PHP 我如何从第二个数组中找到内部键和值与第一个数组匹配的数组键? - How can I find the array key from second array where the inner keys and values match first array? PHP如何将值从一个数组传递到另一个数组? - PHP How can I pass values from one array to another? 基于列将二维数组合并到另一个二维数组中,并从另一列中的相关数据创建子数组 - Merge 2d array into another 2d array based on a column and create a subarray from related data in another column 我如何找到一个数组的最后一个键,可以从另一个数组中找到一个值? - How can i find the last key of an array where can be found a value from another array? 如何从PHP中的键=&gt;“值的数组”数组变为“键=&gt;值”的数组数组 - How do I go from an array of key => “array of values” to an array of an array of “key => values” in php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM