3

I have two arrays:

$arrKeys = array('str', 'str', 'otherStr');
$arrVals = array('1.22', '1.99', '5.17');

I would like to merge them into something like this

$arrResult = array(
    array('str' => 1.22),
    array('str' => 1.99),
    array('otherStr' => 5.17)
);

The keys are non-unique, otherwise I'd use array_combine. That would give a bit different output, but it would suit me as well.

Can this be done in an elegant way using PHP 5.2.x, without foreach/for loops, preferably using PHP's built-in functions?

2
  • You can't have non-unique keys in a php array. Commented Jan 31, 2012 at 11:35
  • @chelmertz - yes, that is why I'm not using array_combine and the result I would like to obtain is a bit different, as in my example Commented Jan 31, 2012 at 11:36

3 Answers 3

16

You can use array_map:

$arrKeys = array('str', 'str', 'otherStr');
$arrVals = array('1.22', '1.99', '5.17');
function foo($key, $val) {
   return array($key=>$val);
}

$arrResult = array_map('foo', $arrKeys, $arrVals);

print_r($arrResult);
Array
(
    [0] => Array
        (
            [str] => 1.22
        )

    [1] => Array
        (
            [str] => 1.99
        )

    [2] => Array
        (
            [otherStr] => 5.17
        )

)

BTW, if you upgrade to PHP 5.3 you can do this using an anonymous function, which is a bit more elegant:

array_map(function($key, $val) {return array($key=>$val);}, $arrKeys, $arrVals);
Sign up to request clarification or add additional context in comments.

1 Comment

I wonder why would someone down-vote this after it was accepted without adding a comment
10

There is absolutely nothing "elegant" in using PHP's built-in functions and nothing to pursue for.

This is not a Haute Couture show. This is programming.
As long as you have a solution that is sane, readable and works, you can call it elegant.

Simple foreach will do the job. You'd do yourself huge favor and save a lot of time if start using working solutions instead of looking for "elegant" ones.

$result = array();
foreach ($arrKeys as $i => $key) {
  $result[] = array($key => $arrVals[$i]);
}

6 Comments

Isn't array_map simple and elegant?
no. obfuscating your own code has nothing in common with elegance
I have no problems reading array_map, but indeed here I think just a simple loop is a helluvalot clearer for the next guy....
like the way you do it dude.. :)
lol. PHP's built in functions are optimized and compiled in C -- i.e much faster than the hackey crap you will write with your "sane, readable, elegant" PHP code. Use the optimized low level code as much as possible.
|
2

Assuming you want item 0 in $arrKeys and item 0 in $arrVals to be added to one array, and that array to be an item in $arrResult, and so on, the easiest way would probably be a simple loop:

$arrResult = array();
for ($i = 0, $max = count($arrKeys); $i < $max; $i++) {
    $arrResult[] = array($arrKeys[$i], $arrValues[$i]);
}

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.