php looping through multiple arrays

15,535

Solution 1

If they have the same keys you can just loop through the keys and use them to index the arrays using array_keys:

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

If you're worried about some keys not existing you can try (e.g.) array_key_exists($key,$array_two).

Solution 2

$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])
    {
        ...
    }
}

It's a little bit superfluous, really, but I see a certain beauty in it.

Solution 3

You can easily do it 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);
        }
    }
}

Solution 4

$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";
    }
}

output:
equal key:c

other one is better lol.

Solution 5

There may be better ways, but this will iterate 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);
}
Share:
15,535
jkdba
Author by

jkdba

I am here to learn and help when and where I can.

Updated on July 27, 2022

Comments

  • jkdba
    jkdba almost 2 years

    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 loop through both of these arrays simultaneously so I can make simple comparisons. I looked at using a foreach loop but I can only process the information one array at a time. I also looked at merging the arrays but I can not see the use in doing this since I need both the keys and values to make the comparisons. does anyone have a solution to this problem? I appreciate your time in advanced.

    to be specific on the comparisons i want to something to this extent

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

    Would it be possible to use recursion to loop instead of using and iterative loop?

  • jkdba
    jkdba about 12 years
    this would output $array_two three time though
  • Top Questions
    Top Questions almost 10 years
    this...is just beautiful...
  • xPheRe
    xPheRe over 9 years
    I really like the power of SPL iterators. This is called 'zip' in functional programming. I'm hoping for even more awesome iterators and utilities in the PHP future.
  • Beta Projects
    Beta Projects over 9 years
    Should be foreach, not for.
  • Niels Keurentjes
    Niels Keurentjes about 8 years
    I love the code porn facet of this.