Dan P.
Dan P.

Reputation: 1775

Create an associative array out of an array by using values that come from another array

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?

Upvotes: 1

Views: 34

Answers (1)

Daan
Daan

Reputation: 12236

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);

Upvotes: 3

Related Questions