php循环遍历多个数组[重复]

时间:2022-10-19 09:49:43

This question already has an answer here:

这个问题在这里已有答案:

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.

我希望能够同时遍历这两个数组,这样我就可以进行简单的比较。我查看了使用foreach循环,但我一次只能处理一个数组的信息。我也考虑过合并数组,但由于我需要键和值来进行比较,所以我无法看到这样做的用法。有没有人有解决这个问题的方法?我很感激你的进步时间。

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?

是否可以使用递归来循环而不是使用和迭代循环?

8 个解决方案

#1


15  

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

如果它们具有相同的键,您只需循环键并使用它们使用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).

如果您担心某些密钥不存在,您可以尝试(例如)array_key_exists($ key,$ array_two)。

#2


18  

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

这真的有点多余,但我看到它有一定的美感。

#3


2  

You can easily do it with foreach.

您可以使用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);
        }
    }
}

#4


1  

$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

输出:等于键:c

other one is better lol.

另一个是更好的哈哈。

#5


1  

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

我查看了使用foreach循环,但我一次只能处理一个数组的信息。

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

如果两个数组中的键相同,则可以使用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]);
}

#6


1  

/* 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;
   }

#7


1  

<?php
foreach($array_one as $k => $v)
{
  $result = $v . $array_two[$k];
}
?>

#8


1  

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.

可能有更好的方法,但这将遍历两个数组,使用foreach进行array_one和reset,next,并键入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


15  

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

如果它们具有相同的键,您只需循环键并使用它们使用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).

如果您担心某些密钥不存在,您可以尝试(例如)array_key_exists($ key,$ array_two)。

#2


18  

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

这真的有点多余,但我看到它有一定的美感。

#3


2  

You can easily do it with foreach.

您可以使用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);
        }
    }
}

#4


1  

$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

输出:等于键:c

other one is better lol.

另一个是更好的哈哈。

#5


1  

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

我查看了使用foreach循环,但我一次只能处理一个数组的信息。

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

如果两个数组中的键相同,则可以使用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]);
}

#6


1  

/* 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;
   }

#7


1  

<?php
foreach($array_one as $k => $v)
{
  $result = $v . $array_two[$k];
}
?>

#8


1  

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.

可能有更好的方法,但这将遍历两个数组,使用foreach进行array_one和reset,next,并键入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);
}