0

I have 2 arrays, they are the same size, and each element corresponds to the same item in each array, ie, arrays look like

array1 => 'name', 'description', 'value' 
array2 => 'fred', 'manager', '100000'

So its basically a 2D array but they are separate due to the way they are built.

How do I iterate through them together so that I can join them like

name=fred, description=manager, 

all in the same loop ?

EDIT Just realised i may have 3 arrays, array1, array2, array3, I assume that eliminates array_combine as an option ?

makapaka
  • 169
  • 5
  • 16
  • 1
    You can still do that see: http://stackoverflow.com/questions/6553752/array-combine-three-or-more-arrays-with-php but I do think other methods are easier – Mramaa Jan 04 '15 at 11:28

3 Answers3

1

I suggest you to use array_combine PHP function.

Ziumin
  • 4,800
  • 1
  • 27
  • 34
1
for($x=0;$x<count($array1);$x++) {
$element1 = $array1[$x];
$element2 = $array2[$x];
//Now you can use these elements
}

or use array_combine()

$array3 = array_combine($array1,$array2);
for($x-0;$x<count($array3);$x++){
$element2 = $array3[$x];
$element1 = key($array[$x]);
}

or

$array3 = array_combine($array1,$array2);
foreach ($array3 as $element1 => $element2){
//Do what you want
}

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

What you prefer find/easiest...

Mramaa
  • 400
  • 2
  • 13
  • 1
    Note that if you use `array_combine`, you can use the `foreach` loop instead. – Max Jan 04 '15 at 11:09
0
$array3 = array_combine($array1,$array2);
JimboSlice
  • 692
  • 4
  • 13