0

I having two arrays say,

$array1 = array("code" => "E0089",
                "desc" => "some description");

$array2 = array("code" => "Code",
                "desc" => "Description");

resultant array should be,

$result = array("Code" => "E0089",
                "Description" => "some description");

In short, I want to map two arrays and create third array.

0

2 Answers 2

2

short and simple: array_combine()

just use it like this:

$result = array_combine($array2, $array1);
Sign up to request clarification or add additional context in comments.

1 Comment

Very nice but I think the OP actually wanted to map keys and values instead of relying on position in the array
1

If the arrays are already in right order:

$result = array_combine($array2, $array1);

Otherwise, you will need to do some looping:

$result = array();
foreach ($array2 as $key => $val)
  $result[$val] = $array1[$key];

2 Comments

Might want to do some checking for array_key_exists($key, $array1)
@Phil, I don't disagree. Left out for the sake of brevity. Got to leave something for the OP to do. ;)

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.