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 ?

1

3 Answers 3

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...

Sign up to request clarification or add additional context in comments.

1 Comment

Note that if you use array_combine, you can use the foreach loop instead.
1

I suggest you to use array_combine PHP function.

Comments

0
$array3 = array_combine($array1,$array2); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.