rubber boots rubber boots - 1 year ago 55
PHP Question

Return a single Element from an Associative Array from a Function

Lets assume we have two PHP-Arrays:

$some_array = array('a','b','c','d','e','f');
$another_array = array(101,102,103,104,105,106);

In PHP, there are already some Array-Functions that allow the construction of an Associative Array (AKA hash), e,g,;

$hash = array_combine(

And here it comes. What should I do if I want to create a hash in a more functional style, if I want to compute key and value on the fly and build the hash through a map-operation, like (not working):

# wishful thinking
$hash = array_map(
function($a, $b){ return ($a => $b); },

The problem here seems to be the expectation, the line

function($a, $b){ return ($a => $b); }

would indeed return an key value/pair of a hash - which it doesn't.

Q: How can I return a key/value pair from a function - which can be used to build up an associative array?


To make clear what I really was looking for, I'll provide a perl example of hash generation:

# we have one array of characters (on which our hash generation is based)

my @array = qw{ a b c d e f };

# now *generate* a hash with array contents as keys and
# ascii numbers as values in a *single operation *
# (in Perl, the $_ variable represents the actual array element)

my %hash = map +($_ => ord $_), @array;

Result (%hash):

a => 97
b => 98
c => 99
d => 100
e => 101
f => 102

From the responses, I'd now think this is impossible in PHP. Thanks to all respondends.

Answer Source

EDIT: It's not entirely clear whether you're having a problem merely with returning multiple variables from a function, or whether you're having problems storing a function in an array. Your post gives the impression that storing the function in the array works, so I'll tackle the return-multiple-variables problem.

There is no way to return a single instance of a key/value pair in PHP. You have to have them in an array... but remember that in PHP, an array and hashmap are exactly the same thing. It's weird (and controversial), but that means it's perfectly legitimate to return an array/hashmap with the multiple values you wish to return.

There are only two sane ways that I know (from 10+ years of PHP experience) to get more than one variable out of a function. One is the good'ol fashion way of making the input variable changeable.

function vacuumPackSandwitch(&$a, &$b) {
    $a = 505;
    $b = 707;

This will change both $a and $b as opposed to changing copies of them like usual. For example:

$a = 1;
$b = 2;
vacuumPackSandwitch($a, $b);
print $a.' '.$b;

This will return "505 707", not "1 2" like normally. You might have to do:

vacuumPackSandwitch(&$a, &$b);

But if that's the case, the PHP compiler will duly let you know.

The other way is to return an array, which I suppose is the clearer and preferred way.

function ($a, $b) {
    return array($a, $b);

You can grab both variables at the same time by doing:

list($c, $d) = vacuumPackSandwitch($a, $b);

Hope it helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download