Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

This code is intended to take an array and a key as inputs, and return an array of the values paired with that key.

function valuelist($array, $array_column) {
    $return = array();
    foreach($array AS $row){
        $return[]=$row[$array_column];
    };
    return $return;
};

Can this be improved in terms of processing speed?

share|improve this question

4 Answers 4

I was able to play around with your requirements, and I think I got a solution.

Here is what I did to profile your code.

I set up the data array with:

$new = array(
    array('a' => 1,
        'b' => 2),
    array('a' => 3,
        'b' => 4),
    array('a' => 5,
        'b' => 6),
);

Calling it like so:

valuelist($new, 'b');

to produce:

Array
(
    [0] => 2
    [1] => 4
    [2] => 6
)

And the average function time was about 2.1894 E-6 seconds.

However, PHP 5.5 introduced us to the array_column function! And it happens to do exactly what you want! Yipee!

We can do a big swap-out, and come up with this:

function valuelist($array, $array_column) {
    return array_column($array, $array_column);
}

Now when we do the 50k calls, we learn that now the average function time is about 2.6226 E-10 seconds.

However, there is a little overhead due to the user function applied over top of the built-in function. By just calling array_column without the function does reduce the processing time, but by an amount so small it's hard to even say it's an amount! Just for the sake of cleanliness, I'd remove the user function anyways.

Don't tell me that's not an improvement!

However, without this function, I was unable to get a faster execution. I tried array_filter, array_map, and array_walk. Using use for the closure, as that proved faster than array_map's third parameter.

share|improve this answer
1  
Note that it's pointless to create an exact wrapping function like this; all you do is add the (admittedly small) overhead of a function call. –  Schism Jul 24 at 21:09
    
Unlucky for me, I'm on PHP 5.3 –  Jeromy French Jul 25 at 2:46
1  
Well after looking around a little, there are libraries to replace the functionality, but it all boils down to what you are currently doing. Sorry about that. If you handle your server's PHP version, updating it is very simple. If you're on a shared host (or a company's server), I suggest you tell your provider to update ASAP. –  Alex L Jul 25 at 2:57
    
@AlexL: Have you compared the avg function call of array_column when it's not wrapped in this valuelist function? The overhead of a custom function, that just calls an existing one seems silly to me... –  Elias Van Ootegem Jul 25 at 7:50

Two minor, not performance related notes:

  1. Spacing is not consistent around =s:

    $return = array();
    ...
    $return[]=$row[$array_column];
    

    The following would a little bit easier to read:

     $return = array();
     ...
     $return[] = $row[$array_column];
    
  2. I would rename $return to $result. Using a reserved keyword (return) as a variable name a little bit weird.

share|improve this answer

Basically, I side with AlexL on this one, but seeing as you don't have array_column at your disposal, you can still get this job done with a one-liner (sort of):

$a = array(
    array('a' => 1,
        'b' => 2),
    array('a' => 3,
        'b' => 4),
    array('a' => 5,
        'b' => 6),
);

$b = array_map(function($sub)
{
    return $sub['b'];
}, $a);
var_dump($b);

Just thought I'd add an example that works on PHP 5.3, too

share|improve this answer

I don't think you can go faster than that, I would test if the key actually exist when you go through your array to avoid notices that would slow your script and unwanted null values in your array. The way to go is to use the array_column() function in PHP 5.5 and use the userland implementation of if until you can get 5.5 n your server. Simply because array_column() is in C and will always be orders of magnitude faster than userland code.

Php 5.3 is almost at end of life now, the last minor version should be in August and then it will no longer be supported by the PHP project, so you will probably have to upgrade your server for security reason soon. If you can't upgrade right now for 5.5, maybe 5.4 would be an option in the short term, it doesn't have the array_column() function but it is generally speaking a lot faster than the old 5.3 and may speed up your script.

I would write your script that way btw (slight syntax changes to be more PSR compliant, also more white space):

function valuelist($array, $array_column) 
{
    $results = [];

    foreach ($array as $row) {
        if (isset(row[$array_column])) {
            $results[] = $row[$array_column];
        }
    }

    return $results;
}
share|improve this answer
1  
Initializing an array with square brackets became supported in PHP 5.4, therefore since the OP can only use 5.3, your example has a parse error. –  Alex L Jul 25 at 17:31
    
Good point, yet another reason to upgrade ;) –  Pascalc Jul 25 at 18:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.