1

I have two arrays with matching keys and I need to merge the values of both into a new array. I'm beating myself up trying to figure this out. How can I do this?

$options = array(
    "0" => true,
    "1" => true,
    "2" => false,
    "3" => true
);

$columns = array(
    "0" => "first",
    "1" => "last",
    "2" => "id",
    "3" => "group"
);

$what_I_need = array(
    "first" => true,
    "last" => true,
    "id" => false,
    "group" => true
);
0

3 Answers 3

7
$whatYouNeed = array_combine($columns, $options);

That's assuming the arrays are in the correct order. Otherwise:

$whatYouNeed = array();
foreach ($columns as $key => column) {
    $whatYouNeed[$column] = $options[$key];
}
Sign up to request clarification or add additional context in comments.

4 Comments

$whatYouNeed !!! You win for playing off my what_i_need!
@Peter lol, i got a good laugh out of it
@dagon I feel for you... But deceze cracked me up and gave me the answer... what would you of done?
C'est la vie. I wont lose any sleep over it.
2
$what_I_need = array_combine($columns , $options);

array_combine

Comments

2
array array_combine ( array $keys , array $values );

$newArray = array_combine($columns, $options);
print_r($newArray);

should give you:

array(
    "first" => true,
    "last" => true,
    "id" => false,
    "group" => true
);

PHP array_combine

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.