通过多个阵列PHP循环 [英] php looping 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);
我希望能够遍历这两个阵列同时这样我就可以进行简单的比较。我看着使用foreach循环,但我只能一次处理信息的一个数组。我又看了看合并的阵列,但我看不到这样做的使用,因为我同时需要键值,以使比较。没有任何人有一个解决这个问题?我AP preciate您先进的时间。
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?
推荐答案
如果他们有相同的键就可以使用的 array_keys
:
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]
}
如果你担心某些键不存在,你可以尝试(例如) array_key_exists($键,$ array_two)
。
If you're worried about some keys not existing you can try (e.g.) array_key_exists($key,$array_two)
.
这篇关于通过多个阵列PHP循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!