PHP数组。基于关键分简单 [英] PHP array. Simple split based on key

查看:137
本文介绍了PHP数组。基于关键分简单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这更多的是一种用于快速忠告要求的,我觉得很难相信没有手头的任务本地函数。考虑以下几点:

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:

array =>
(0) => "fudge",
(1) => "toffee",
(2) => "caramel"
)

我知道这是实现递归原始数组会很简单,但我不知道是否有一个更简单的实现最终结果的方式。我环顾四周互联网和PHP手册并不能找到任何

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

感谢您能阅读此问题
西蒙

Thank you kindly for reading this question Simon

推荐答案

如果您使用的是PHP 5.3,你可以使用的 array_map [文件] 是这样的:

If you are using PHP 5.3, you can use array_map [docs] like this:

$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'];
}

您绝对不必使用递归。

这篇关于PHP数组。基于关键分简单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆