简体   繁体   English

PHP数组。 基于键的简单拆分

[英]PHP array. Simple split based on key

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 我在Internet和PHP手册上四处张望,找不到任何东西

Thank you kindly for reading this question Simon 谢谢您阅读此问题西蒙

为此有一个本机的array_column()函数(自PHP 5.5起):

$column = array_column($arr, 'groupname');

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. 您绝对不必使用递归。

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 http://sandbox.onlinephpfunctions.com/code/3a8f443283836468d8dc232ae8cc8d11827a5355

Use array_map : 使用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 );

http://codepad.org/7kNi8nNm http://codepad.org/7kNi8nNm

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. 据我所知,PHP中没有一个简单的函数可以为您解决这个问题,数组结构是用户定义的,因此您必须遍历数组。 The easiest way I can think of would be something like this: 我能想到的最简单的方法是这样的:

for($i = 0;$i < count($main_array);$i++){
    $groupname_array[] = $main_array[$i]["groupname"];
}
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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM