[英]Iterating over second array inside foreach loop
I have one flat array that I use to generate a tree later on: 我有一个平面数组,可用于稍后生成树:
[0] => array
[0] => 1
[1] => 3
[1] => array
[0] => 3
[1] => 5
[2] => array
[0] => 8
[1] => 12
[3] => array
[0] => 4
[1] => 7
The values in this array are plain id's, I want to convert them into full names. 此数组中的值是纯id,我想将它们转换为全名。 I can get those names from the database, with corresponding id's, so that's my sample output array:
我可以从数据库中获得这些名称以及相应的ID,因此这是我的示例输出数组:
[0] => array
['id'] => 1
['name'] => 'sample name'
[1] => array
['id'] => 2
['name'] => 'foo'
[2] => array
['id'] => 3
['name'] => 'bar'
So now I have to iterate over the first array, and compare each value to the value from the second array...How can I do that without using foreach loop in every iteration of the external loop? 所以现在我必须遍历第一个数组,并将每个值与第二个数组中的值进行比较...如何在不使用外部循环的每次迭代中使用foreach循环的情况下做到这一点?
Thanks! 谢谢!
Rebuild the 2nd array so you can reference it in the first. 重建第二个数组,以便您可以在第一个中引用它。
$newarr = array();
foreach ($secondval as $val )
{
$newarr[ $val->id ] = $val->name;
}
Then use this in the first loop. 然后在第一个循环中使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.