Jump to content

matching up array values


real_illusions
 Share

Recommended Posts

Sorry, you're probably getting bored of foreach posts by now :)Anyway..I have an array, where in array[1] I have some values that match up to values in array[2].How do loop through the array in a way I can echo out like:arrray1value: array2valuein a nice list.thanks :)

Link to comment
Share on other sites

is it just a one to one relationship between indexes in array1 and array2? If so (i don't think it matters if its one to many, just depends on how you want the outcome to looklike), in your foreach, as iterate over each index/key in array1, run a nested loop checking the second array for all key's who's value matches the value of the key of array1 (which you are in, from the first foreach).

Edited by thescientist
Link to comment
Share on other sites

If the two arrays are numeric and are the same length, then you can just use a for loop:
for ($i = 0; $i < count($ar1); $i++){  echo $ar1[$i];  echo $ar2[$i];}

Unless the matching values are at different indexes...You might be able to use array_map but I think it will probably behave similar to JSG's loop where it loops through both arrays at the same time passing in only the values at the index it's currently on.scientist's approach might be the easiest.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...