4

In php is there a way to get an element from each sub array without having to loop - thinking in terms of efficiency.

Say the following array:

$array = array(
    array(
        'element1' => a,
        'element2' => b
    ),
    array(
        'element1' => c,
        'element2' => d
    )
);

I would like all of the 'element1' values from $array

8
  • Looping is going to be a lot more efficient than any alternatives, unless you know exactly how many entries are in the top-level of your array and you want to hard-code a line of code for every element.... so 200 entries = 200 lines of code, with no option to change the number of entries without changing your code, and saves you all of 2.75 picoseconds to avoid looping - bad on a whole load of counts, good on none Commented Apr 25, 2013 at 10:23
  • 1
    200 lines of code even pasting will take more than 2.75 picoseconds. Unless there is some specific logic behind this which wasn't stated in the question, there is no point in not looping. Commented Apr 25, 2013 at 10:26
  • How do you want the 'element1's returned to you? An array? A string? Commented Apr 25, 2013 at 10:29
  • @PhillSparks not important as I can deal with that later in my code. Commented Apr 25, 2013 at 10:30
  • If you're already running PHP 5.5, then the new array_column() feature can give you this Commented Apr 25, 2013 at 10:36

4 Answers 4

4

There are a number of different functions that can operate on arrays for you, depending on the output desired...

$array = array(
    array(
       'element1' => 'a',
       'element2' => 'b'
   ),
   array(
       'element1' => 'c',
       'element2' => 'd'
   )
);

// array of element1s : array('a', 'c')
$element1a = array_map(function($item) { return $item['element1']; }, $array);

// string of element1s : 'ac'
$element1s = array_reduce($array, function($value, $item) { return $value . $item['element1']; }, '');

// echo element1s : echo 'ac'
array_walk($array, function($item) {
    echo $item['element1'];
});

// alter array : $array becomes array('a', 'c')
array_walk($array, function(&$item) {
    $item = $item['element1'];
});

Useful documentation links:

Sign up to request clarification or add additional context in comments.

Comments

2

You can use array_map.

Try code below...

$arr = $array = array(
    array(
       'element1' => a,
       'element2' => b
   ),
   array(
       'element1' => c,
       'element2' => d
   )
);

print_r(array_map("getFunc", $arr));

function getFunc($a) 
{ 
    return $a['element1']; 
}

See Codepad.

But I think array_map will also use loop internally.

2 Comments

I prefer this notation: $func = function(){echo $a['element1']."\n";} array_map($func, $arr); (working as of PHP 5.3).
Why array_map? array_map's callbacks should return a value, and array_map itself will then return an array. Your example here would be better with array_walk
2

If you're running PHP 5.5 (currently the beta-4 is available), then the following

$element1List = array_column($array, 'element1');

should give $element1List as an simple array of just the element1 values for each element in $array

$array = array(
    array(
       'element1' => a,
       'element2' => b
   ),
   array(
       'element1' => c,
       'element2' => d
   )
);

$element1List = array_column($array, 'element1');
print_r($element1List);

gives

Array
(
    [0] => a
    [1] => c
)

Comments

0

Without loop? Recursion!

$array = array(
        array(
                'element1' => 'a',
                'element2' => 'b'
        ),
        array(
                'element1' => 'c',
                'element2' => 'd'
        )
);

function getKey($array,$key,$new = array()){
    $count = count($array); 
    $new[] = $array[0][$key];
    array_shift($array);

    if($count==1)
        return $new;

    return getKey($array,$key,$new);
}

print_R(getKey($array,'element1'));

As I understood from Wikipedia Recursion is not a loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.