HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Returns values for a specific array and key

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
arrayandforreturnsspecificvalueskey

Problem

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

Solution

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.

Code Snippets

$new = array(
    array('a' => 1,
        'b' => 2),
    array('a' => 3,
        'b' => 4),
    array('a' => 5,
        'b' => 6),
);
valuelist($new, 'b');
Array
(
    [0] => 2
    [1] => 4
    [2] => 6
)
function valuelist($array, $array_column) {
    return array_column($array, $array_column);
}

Context

StackExchange Code Review Q#57958, answer score: 6

Revisions (0)

No revisions yet.