# Returns values for a specific array and key

This PHP (5.3) 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? ‡-For example, given: $get_role_action = array(
array("ACTION_CD" => "RETURN_PETITION"),
array("ACTION_CD" => "UNLOCK_RECORD"),
array("ACTION_CD" => "ACKNOWLEDGE"),
array("ACTION_CD" => "REQUEST_POST_ACTION"),
array("ACTION_CD" => "CHANGE_REQUEST_TYPE")
);


$variables['role_action_list']=valuelist($get_role_action, 'ACTION_CD'); should yield:

["role_action_list"]=>
array(17) {
[0]=>
string(15) "RETURN_PETITION"
[1]=>
string(13) "UNLOCK_RECORD"
[2]=>
string(11) "ACKNOWLEDGE"
[3]=>
string(19) "REQUEST_POST_ACTION"
[4]=>
string(19) "CHANGE_REQUEST_TYPE"
}

-

## 4 Answers

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.

-
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 '14 at 21:09
Unlucky for me, I'm on PHP 5.3 –  Jeromy French Jul 25 '14 at 2:46
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 '14 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 '14 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.

-

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

-
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 '14 at 17:31
Good point, yet another reason to upgrade ;) –  Pascalc Jul 25 '14 at 18:17

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

-