1

I have several arrays.

$keysArray = ['Key1', 'Key2'];

$array1 = ['Value1', 'Value2'];

$array2 = ['Value1', 'Value2'];

$array3 = ['Value1', 'Value2'];

I want to make each array (except $keysArray) an associative array in which the keys would come from $keysArray.

So for instance, $array1 $array2 and $array3 would look like

['Key1' => 'Value1', 'Key2' => 'Value2'];

How can I efficiently achieve this?

2
  • Is the index from the keys and values always 1 on 1? Commented Feb 3, 2015 at 10:27
  • Yes, I'm basically taking data from a tab delimited text file and if a column is empty, it will still be part of the array, just empty value. Commented Feb 3, 2015 at 10:28

1 Answer 1

3

From the manual: http://php.net/manual/en/function.array-combine.php

array array_combine ( array $keys , array $values )

So for your example:

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

1 Comment

Hi billy I edited my original post I had accidentally put a "key3" there.

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.