php 获取多维数组中特定列的所有值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17159073/
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
Get all values of a specific column in a multidimensional array
提问by Parijat Kalia
I have a multidimensional array, that has say, x number of columns and y number of rows.
我有一个多维数组,也就是说,x 列数和 y 行数。
I want specifically all the values in the 3rd column.
我特别想要第三列中的所有值。
The obvious way to go about doing this is to put this in a for loop like this
执行此操作的明显方法是将其放入这样的 for 循环中
for(i=0;i<y-1;i++)
{
$ThirdColumn[] = $array[$i][3];
}
but there is an obvious time complexity of O(n) involved here. Is there a built in way for me to simply extract each of these rows from the array without having to loop in.
但是这里有一个明显的 O(n) 时间复杂度。是否有一种内置的方法可以让我简单地从数组中提取这些行中的每一行而无需循环。
For example (this does not work offcourse)
例如(这在课外不起作用)
$ThirdColumn = $array[][3]
回答by Igor Parra
Given a bidimensional array $channels
:
给定一个二维数组$channels
:
$channels = array(
array(
'id' => 100,
'name' => 'Direct'
),
array(
'id' => 200,
'name' => 'Dynamic'
)
);
A nice way is using array_map:
一个不错的方法是使用array_map:
$_currentChannels = array_map(function ($value) {
return $value['name'];
}, $channels);
and if you are a potentate (php 5.5+) through array_column:
如果您是通过array_column的当权者(php 5.5+):
$_currentChannels = array_column($channels, 'name');
Both results in:
两者的结果都是:
Array
(
[0] => Direct
[1] => Dynamic
)
Star guests: array_map(php4+) and array_column(php5.5+)
明星嘉宾: array_map(php4+) 和array_column(php5.5+)
// array array_map ( callable $callback , array $array1 [, array $... ] )
// array array_column ( array $array , mixed $column_key [, mixed $index_key = null ] )
回答by hakre
Is there a built in way for me to simply extract each of these rows from the array without having to loop in.
是否有一种内置的方法可以让我简单地从数组中提取这些行中的每一行而无需循环。
Not yet. There willbe a function soon named array_column()
. However the complexity will be the same, it's just a bit more optimized because it's implemented in C and inside the PHP engine.
还没有。这里将是一个即将命名的功能array_column()
。但是复杂度是一样的,只是稍微优化了一点,因为它是用 C 语言和 PHP 引擎实现的。
回答by Kylie
Try this....
尝试这个....
foreach ($array as $val)
{
$thirdCol[] = $val[2];
}
Youll endup with an array of all values from 3rd column
你最终会得到第 3 列中所有值的数组
回答by 1ry
You could try this:
你可以试试这个:
$array["a"][0]=10;
$array["a"][1]=20;
$array["a"][2]=30;
$array["a"][3]=40;
$array["a"][4]=50;
$array["a"][5]=60;
$array["b"][0]="xx";
$array["b"][1]="yy";
$array["b"][2]="zz";
$array["b"][3]="aa";
$array["b"][4]="vv";
$array["b"][5]="rr";
$output = array_slice($array["b"], 0, count($array["b"]));
print_r($output);
回答by Muc
Another way to do the same would be something like $newArray = array_map( function($a) { return $a['desiredColumn']; }, $oldArray );
though I don't think it will make any significant (if any) improvement on the performance.
另一种做同样事情的方法是,$newArray = array_map( function($a) { return $a['desiredColumn']; }, $oldArray );
尽管我认为它不会对性能产生任何重大(如果有的话)改进。