I have 2 Multidimensional arrays as follow:
Array1:
Array (
[0] => Array (
[0] => 2D Design
[1] => 3D Design & Modeling)
[1] => Array ( [0] => Android Developer
[1] => Artificial Intelligence
[2] => Web Developer)
)
Array2:
Array (
[0] => Array (
[0] => 5
[1] => 10)
[1] => Array ( [0] => 2
[1] => 4
[2] => 6)
)
I want to combine the above 2 arrays as key
and value
as below.
Array (
[0] => Array (
[2D Design] => 5
[3D Design & Modeling] => 10 )
[1] => Array (
[Android Developer] => 2
[Artificial Intelligence] => 4
[Web Developer] => 6 )
)
Please help me to do this. Answers will be appreciated.
3
Answers
use
array_combine()
function creates an array by using the elements from one “keys” array and one “values” array.Note: Both arrays must have equal number of elements!
First parameter array taken as key of new array and second parameter taken as value new array .
Output :
This will work,
I have fetched values of first array
arr1
as key totemp
variable and map it with values ofarr2
as value totemp
array.This code will work even if index i.e. 0,1,2,3 can be anything.
Here is working code.
Simply make mapped calls of
array_combine()
. So long as the same positioned rows have the same number of elements in them, everything will work perfectly.Code: (Demo)