Running php through multiple arrays

okay so i have two arrays

$array_one([a]=>2,[b]=>1,[c]=>1); $array_two([a]=>1,[b]=>2,[c]=>1); 

I want to be able to scroll through both of these arrays at the same time so that I can make simple comparisons. I looked at using a foreach loop, but I can only process information on one array at a time. I also looked at merging arrays, but I see no way to use this, since I need both keys and values โ€‹โ€‹for comparison. Does anyone have a solution to this problem? I appreciate your time in the forefront.

to be specific in comparison, I want something in this sense

 if ($keyone == $keytwo && $valuetwo <= $valueone) { print_r ($array_two); } 

Is it possible to use recursion for a loop instead of using an iterative loop?

+7
source share
8 answers

If they have the same keys, you can simply scroll through the keys and use them to index arrays using array_keys :

 foreach(array_keys($array_one) as $key) { // do something with $array_one[$key] and $array_two[$key] } 

If you are concerned about some non-existing keys, you can try (for example) array_key_exists($key,$array_two) .

+15
source
 $array_one = array ( 'a' => 2, 'b' => 1, 'c' => 1 ); $array_two = array ( 'a' => 1, 'b' => 2, 'c' => 1 ); $iterator = new MultipleIterator (); $iterator->attachIterator (new ArrayIterator ($array_one)); $iterator->attachIterator (new ArrayIterator ($array_two)); foreach ($iterator as $item) { if ($item [0] > $item [1]) { ... } } 

This is a little superfluous, really, but I see a certain beauty in it.

+18
source

You can easily do this with foreach.

 $array_one([a]=>2,[b]=>1,[c]=>1); $array_two([a]=>1,[b]=>2,[c]=>1); foreach($array_one as $array_one_key => $array_one_value) { foreach($array_two as $array_two_key => $array_two_value) { if ($array_one_key == $array_two_key && $array_two_value <= $array_one_value) { print_r ($array_two); } } } 
+2
source
 $array_one = array('a'=>2,'b'=>1,'c'=>1); $array_two = array('a'=>1,'b'=>2,'c'=>1); $keys = array_keys($array_one); for($x=0;$x<sizeof($array_one);$x++){ if($array_one[$keys[$x]] == $array_two[$keys[$x]]) { echo "equal key:".$keys[$x]."\n"; } } 

Exit:
equal key: c

the other is better lol.

+1
source

I looked at using a foreach loop, but I can only process information on one array at a time.

If the keys in both arrays are the same, you can use foreach ():

 foreach($array_one as $key => $value) { // do something to the first array $array_one[$key] = doSomething($value); // do something else to the second array $array_two[$key] = doSomethingElse($array_two[$key]); } 
+1
source
 /* Make sure the array pointer is in the first position. */ reset($leftarray); reset($rightarray); /* Loop through the arrays. */ $i=0; while ($i < $totalitems) {echo current($leftarray)."<br>". current($rightarray)."<br>"; next($leftarray); next($rightarray); $i=$i+1; } 
+1
source
 <?php foreach($array_one as $k => $v) { $result = $v . $array_two[$k]; } ?> 
+1
source

There may be more efficient ways, but this will go through both arrays using foreach for array_one and reset, next and key for array_two.

 $array_one = array('a'=>2,'b'=>1,'c'=>1,'x'=>3,'y'=>4); $array_two = array('a'=>1,'b'=>2,'c'=>1,'d'=>3,'e'=>8); $v2 = reset($array_two); $k2 = key($array_two); foreach ($array_one as $k1 => $v1) { if ($k1 == $k2 && $v1 == $v2 ) { echo "$k1 == $k2 && $v1 == $v2\n"; } elseif ($k1 == $k2) { echo "$k1 == $k2 Keys match\n"; } elseif ($v1 == $v2) { echo "$v1 == $v2 Values match\n"; } else { echo "$k1 $v1 $k2 $v2 No match\n"; } $v2 = next($array_two); $k2 = key($array_two); } 
+1
source

All Articles