This is more a request for a quick piece of advice as, I find it very hard to believe there isn't a native function for the task at hand. Consider the following:
这更像是对快速建议的要求,因为我发现很难相信手头的任务没有本地功能。考虑以下:
array =>
(0) => array("id" => "1", "groupname" => "fudge", "etc" => "lorem ipsum"),
(1) => array("id" => "2", "groupname" => "toffee", "etc" => "lorem ipsum"),
(2) => array("id" => "3", "groupname" => "caramel", "etc" => "lorem ipsum")
)
What I am looking to get is a new array which uses only "groupname" so would equal:
我期待得到的是一个新的数组,它只使用“groupname”,所以会相等:
array =>
(0) => "fudge",
(1) => "toffee",
(2) => "caramel"
)
I know this is very simple to achieve recursively going through the original array, but I was wondering if there is a much simpler way to achieve the end result. I've looked around the internet and on the PHP manual and can't find anything
我知道实现递归遍历原始数组非常简单,但我想知道是否有更简单的方法来实现最终结果。我环顾了互联网和PHP手册,找不到任何东西
Thank you kindly for reading this question Simon
谢谢你好好读这个问题西蒙
6 个解决方案
#1
6
If you are using PHP 5.3, you can use array_map
[docs] like this:
如果您使用的是PHP 5.3,则可以像这样使用array_map [docs]:
$newArray = array_map(function($value) {
return $value['groupname'];
}, $array);
Otherwise create a normal function an pass its name (see the documentation).
否则创建一个普通函数并传递其名称(请参阅文档)。
Another solution is to just iterate over the array:
另一种解决方案是迭代数组:
$newArray = array();
foreach($array as $value) {
$newArray[] = $value['groupname'];
}
You definitely don't have to use recursion.
你绝对不必使用递归。
#2
4
There is a native array_column() function exactly for this (since PHP 5.5):
完全有一个本机array_column()函数(从PHP 5.5开始):
$column = array_column($arr, 'groupname');
#3
1
Use array_map
:
<?php
$a = array(
array("id" => "1", "groupname" => "fudge", "etc" => "lorem ipsum"),
array("id" => "2", "groupname" => "toffee", "etc" => "lorem ipsum"),
array("id" => "3", "groupname" => "caramel", "etc" => "lorem ipsum")
);
function get_groupname( $arr )
{
return $arr['groupname'];
}
$b = array_map( 'get_groupname', $a );
var_dump( $a );
var_dump( $b );
#4
1
Values by all array keys:
所有数组键的值:
$array = [
["id"=>"1","user"=>"Ivan"],
["id"=>"2","user"=>"Ivan"],
["id"=>"3","user"=>"Elena"]
];
$res = [];
$array_separate = function($value, $key) use (&$res){
foreach($value AS $k=>$v) {
$res[$k][] = $v;
}
};
array_walk($array, $array_separate);
print_r($res);
result:
Array
(
[id] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[user] => Array
(
[0] => Ivan
[1] => Ivan
[2] => Elena
)
)
http://sandbox.onlinephpfunctions.com/code/3a8f443283836468d8dc232ae8cc8d11827a5355
#5
0
As far as I know there isn't a simple function in PHP that could handle this for you, the array structure is user-defined, you would have to loop through the array. The easiest way I can think of would be something like this:
据我所知,PHP中没有一个可以为你处理的简单函数,数组结构是用户定义的,你必须遍历数组。我能想到的最简单的方法是这样的:
for($i = 0;$i < count($main_array);$i++){
$groupname_array[] = $main_array[$i]["groupname"];
}
#6
0
function array_get_keys(&$array, $key) {
foreach($array as &$value) $value = $value[$key];
}
array_get_keys($array, 'groupname');
or
function array_get_keys(&$array, $key) {
$result = Array();
foreach($array as &$value) $result[] = $value[$key];
return $result;
}
$new_array = array_get_keys($array, 'groupname');
#1
6
If you are using PHP 5.3, you can use array_map
[docs] like this:
如果您使用的是PHP 5.3,则可以像这样使用array_map [docs]:
$newArray = array_map(function($value) {
return $value['groupname'];
}, $array);
Otherwise create a normal function an pass its name (see the documentation).
否则创建一个普通函数并传递其名称(请参阅文档)。
Another solution is to just iterate over the array:
另一种解决方案是迭代数组:
$newArray = array();
foreach($array as $value) {
$newArray[] = $value['groupname'];
}
You definitely don't have to use recursion.
你绝对不必使用递归。
#2
4
There is a native array_column() function exactly for this (since PHP 5.5):
完全有一个本机array_column()函数(从PHP 5.5开始):
$column = array_column($arr, 'groupname');
#3
1
Use array_map
:
<?php
$a = array(
array("id" => "1", "groupname" => "fudge", "etc" => "lorem ipsum"),
array("id" => "2", "groupname" => "toffee", "etc" => "lorem ipsum"),
array("id" => "3", "groupname" => "caramel", "etc" => "lorem ipsum")
);
function get_groupname( $arr )
{
return $arr['groupname'];
}
$b = array_map( 'get_groupname', $a );
var_dump( $a );
var_dump( $b );
#4
1
Values by all array keys:
所有数组键的值:
$array = [
["id"=>"1","user"=>"Ivan"],
["id"=>"2","user"=>"Ivan"],
["id"=>"3","user"=>"Elena"]
];
$res = [];
$array_separate = function($value, $key) use (&$res){
foreach($value AS $k=>$v) {
$res[$k][] = $v;
}
};
array_walk($array, $array_separate);
print_r($res);
result:
Array
(
[id] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[user] => Array
(
[0] => Ivan
[1] => Ivan
[2] => Elena
)
)
http://sandbox.onlinephpfunctions.com/code/3a8f443283836468d8dc232ae8cc8d11827a5355
#5
0
As far as I know there isn't a simple function in PHP that could handle this for you, the array structure is user-defined, you would have to loop through the array. The easiest way I can think of would be something like this:
据我所知,PHP中没有一个可以为你处理的简单函数,数组结构是用户定义的,你必须遍历数组。我能想到的最简单的方法是这样的:
for($i = 0;$i < count($main_array);$i++){
$groupname_array[] = $main_array[$i]["groupname"];
}
#6
0
function array_get_keys(&$array, $key) {
foreach($array as &$value) $value = $value[$key];
}
array_get_keys($array, 'groupname');
or
function array_get_keys(&$array, $key) {
$result = Array();
foreach($array as &$value) $result[] = $value[$key];
return $result;
}
$new_array = array_get_keys($array, 'groupname');