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]
5 个解决方案
#1
17
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的potentate (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 ] )
#2
2
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 will be 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引擎中实现的。
#3
1
Try this....
试试这个....
foreach ($array as $val)
{
$thirdCol[] = $val[2];
}
Youll endup with an array of all values from 3rd column
你将具有来自第三列的所有值的数组
#4
0
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);
#5
0
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(函数($a)返回$a['desiredColumn']];},oldArray美元);虽然我不认为它会对性能有任何显著的改善。
#1
17
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的potentate (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 ] )
#2
2
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 will be 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引擎中实现的。
#3
1
Try this....
试试这个....
foreach ($array as $val)
{
$thirdCol[] = $val[2];
}
Youll endup with an array of all values from 3rd column
你将具有来自第三列的所有值的数组
#4
0
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);
#5
0
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(函数($a)返回$a['desiredColumn']];},oldArray美元);虽然我不认为它会对性能有任何显著的改善。