PHP foreach循环多个对象? [英] PHP foreach loop on multiple objects?

查看:237
本文介绍了PHP foreach循环多个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PHP / codeigniter,通常循环查询结果我会做这样的:

I am using PHP/codeigniter, and typically to loop over query results I would do something like this:

<?php foreach($data as $row):?>
//html goes here
<?php endforeach;?>

但是,我有另一个对象数组, $ data2 ,与 $ data 具有相同数量的对象:是否有任何有效的PHP语法,将允许我同时访问 rows 在同一个循环中的两个对象使用 foreach 语句?我知道我可以通过访问数组的空格,定期循环的循环,但这不会那么干净。

But, say I have another object array, $data2, with the same number of objects as $data: is there any valid PHP syntax that would allow me to simultaneously access rows on both objects within the same loop using the foreach statement? I know I could do this with a regular for loop by accessing the indeces of the arrays, but this wouldn't be as clean.

推荐答案

你可以这样:

<?php foreach($data as $key => $row): $row2 = $data2[$key]; ?>
//html goes here
<?php endforeach;?>

这将有效地使您能够同时循环两个数组。

Which would effectively enable you to loop both arrays at the same time.

这篇关于PHP foreach循环多个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆