8

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"
)

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

Simon
  • 816
  • 2
  • 7
  • 16

6 Answers6

8

There is a native array_column() function exactly for this (since PHP 5.5):

$column = array_column($arr, 'groupname');
flori
  • 14,339
  • 4
  • 56
  • 63
7

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

You definitely don't have to use recursion.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • array_map! thank you very kindly for responding so punctually. Iteration is what i meant to say instead of recursion, apologies. Good stuff :) – Simon Nov 02 '11 at 17:53
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

Sergey
  • 111
  • 1
  • 7
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 );

http://codepad.org/7kNi8nNm

poke
  • 369,085
  • 72
  • 557
  • 602
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:

for($i = 0;$i < count($main_array);$i++){
    $groupname_array[] = $main_array[$i]["groupname"];
}
Jag
  • 1,744
  • 2
  • 11
  • 11
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');
Peter
  • 16,453
  • 8
  • 51
  • 77