是否有从 PHP 中的数组中提取“列”的函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1494953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a function to extract a 'column' from an array in PHP?
提问by Skilldrick
I have an array of arrays, with the following structure :
我有一个数组数组,具有以下结构:
array(array('page' => 'page1', 'name' => 'pagename1')
array('page' => 'page2', 'name' => 'pagename2')
array('page' => 'page3', 'name' => 'pagename3'))
Is there a built-in function that will return a new array with just the values of the 'name' keys? so I'd get:
是否有一个内置函数将返回一个仅包含“名称”键值的新数组?所以我会得到:
array('pagename1', 'pagename2', 'pagename3')
采纳答案by Anti Veeranna
Why does it have to be a built in function? No, there is none, write your own.
为什么它必须是内置函数?不,没有,自己写。
Here is a nice and easy one, as opposed to others in this thread.
这是一个很好且简单的方法,与此线程中的其他方法相反。
$namearray = array();
foreach ($array as $item) {
$namearray[] = $item['name'];
}
回答by John Conde
As of PHP 5.5you can use array_column():
至于PHP 5.5你可以使用array_column():
<?php
$samples=array(
array('page' => 'page1', 'name' => 'pagename1'),
array('page' => 'page2', 'name' => 'pagename2'),
array('page' => 'page3', 'name' => 'pagename3')
);
$names = array_column($samples, 'name');
print_r($names);
回答by fuentesjr
Here's a functional way of doing it:
这是一种实用的方法:
$data = array(
array('page' => 'page1', 'name' => 'pagename1'),
array('page' => 'page2', 'name' => 'pagename2'),
array('page' => 'page3', 'name' => 'pagename3'));
$result = array_map(create_function('$arr', 'return $arr["name"];'), $data);
print_r($result);
回答by papas-source
Well there is. At least for PHP > 5.5.0 and it is called array_column
那么有。至少对于 PHP > 5.5.0,它被称为array_column
The PHP function takes an optional $index_keyparameter that - as per the PHP website - states:
PHP 函数采用一个可选$index_key参数 - 根据 PHP 网站 - 指出:
$index_keyThe column to use as the index/keys for the returned array. This value may be the integer key of the column, or it may be the string key name
$index_key用作返回数组的索引/键的列。这个值可能是列的整数键,也可能是字符串键名
In the answers here, i see a stripped version without the optional parameter. I needed it, so, here is the complete function:
在这里的答案中,我看到了一个没有可选参数的剥离版本。我需要它,所以,这是完整的功能:
if (!function_exists('array_column')) {
function array_column($array, $column, $index_key = null) {
$toret = array();
foreach ($array as $key => $value) {
if ($index_key === null){
$toret[] = $value[$column];
}else{
$toret[$value[$index_key]] = $value[$column];
}
}
return $toret;
}
}
回答by Dinesh
Yes, there is a php built-in function called array_column which does what you are looking for.
是的,有一个名为 array_column 的 php 内置函数可以满足您的需求。
You would call it something like $name_keys = array_column($array, 'name');to get the result that you are looking for.
你会称它为类似$name_keys = array_column($array, 'name');得到你正在寻找的结果。
Please refer to the following entry in the PHP manual for more details:
有关更多详细信息,请参阅 PHP 手册中的以下条目:
回答by Henrik Opel
Similar to fuentesjrs solution, but a bit more generic using array_walk() with a custom callback:
类似于 fuentesjrs 解决方案,但使用 array_walk() 和自定义回调更通用:
// Define the callback
function extract_named_sub_elements(&$item, $key, $name) {
$item = $item[$name];
}
// Test data
$original = array(
array('page' => 'page1', 'name' => 'pagename1'),
array('page' => 'page2', 'name' => 'pagename2'),
array('page' => 'page3', 'name' => 'pagename3'),
);
// Use a copy, as array_walk() operates directly on the passed in array
$copy = $original;
// Substitute 'name' with whatever element you want to extract, e.g. 'page'
array_walk($copy, 'extract_named_sub_elements', 'name');
print_r($copy);
回答by Srok
if (!function_exists('array_column')) {
function array_column($array,$column) {
$col = array();
foreach ($array as $k => $v) {
$col[]=$v[$column];
}
return $col;
}
}
This should work for php versions < 5.5 and degrade in case the function exist
这应该适用于 php 版本 < 5.5 并在函数存在的情况下降级
回答by CrazyMerlin
I wanted to post here, even if this is an old question, because it is still very relevant and many developers do not use PHP >= 5.5
我想在这里发帖,即使这是一个老问题,因为它仍然非常相关并且许多开发人员不使用 PHP >= 5.5
Let's say you have an array like this:
假设你有一个这样的数组:
Array
(
[files] => Array
(
[name] => Array
(
[0] => file 1
[1] => file 2
[2] => file 3
)
[size] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[error] => Array
(
[0] => abc
[1] => def
[2] => ghi
)
)
)
and the output you want is something like this:
你想要的输出是这样的:
Array
(
[0] => Array
(
[0] => file 1
[1] => 1
[2] => abc
)
[1] => Array
(
[0] => file 2
[1] => 2
[2] => def
)
[2] => Array
(
[0] => file 3
[1] => 3
[2] => ghi
)
)
You can simply use the array_map() method without a function name passed as the first parameter, like so:
您可以简单地使用 array_map() 方法,而无需将函数名称作为第一个参数传递,如下所示:
array_map(null, $a['files']['name'], $a['files']['size'], $a['files']['error']);
Unfortunately you cannot map the keys if passing more than one array.
不幸的是,如果传递多个数组,则无法映射键。
回答by VolkerK
You can extend the ArrayIterator classand override the method mixed current(void).
您可以扩展ArrayIterator 类并覆盖该方法mixed current(void)。
class Foo extends ArrayIterator {
protected $index;
public function __construct($array, $index) {
parent::__construct($array);
$this->index = $index;
}
public function current() {
$c = parent::current();
return isset($c[$this->index]) ? $c[$this->index] : null;
}
}
$a = array(
array('page' => 'page1', 'name' => 'pagename1'),
array('name' => '---'),
array('page' => 'page2', 'name' => 'pagename2'),
array('page' => 'page3', 'name' => 'pagename3')
);
$f = new Foo($a, 'page');
foreach($f as $e) {
echo $e, "\n";
}
prints
印刷
page1
page2
page3
回答by Imrul
I don't think there is any need to have a built in function for this. There may be an array in your those array.
我认为没有必要为此内置功能。您的那些数组中可能有一个数组。
$samples=array(
array('page' => 'page1', 'name' => 'pagename1'),
array('page' => 'page2', 'name' => 'pagename2'),
array('page' => 'page3', 'name' => 'pagename3')
);
$output1=array();
$output2=array();
foreach($samples as $sample){
array_push($output1,$sample['name']);
$output2[]=array_splice($sample,1);
}
print_r($output1);
print_r($output2);
in $output1 is the output what you want if you want only to remove the 'page' indexing' part then $output2.
如果您只想删除“页面”索引部分然后 $output2,则 $output1 中的输出是您想要的输出。
if you need all the values from the that array and indexes numerically the array then you can use
如果您需要该数组中的所有值并以数字方式索引该数组,那么您可以使用
$array_1=array_values($samples);
but what i understand, you didn't want this.
但据我所知,你不想要这个。

