如何使用&QUOT多个阵列;单" foreach()循环 [英] How to use multiple arrays in " single " foreach() loop

查看:166
本文介绍了如何使用&QUOT多个阵列;单" foreach()循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想呼应了两个数组中的所有变量用一个foreach循环制作的URL。一些编码例子会受到尊重。

I'm trying to echo out all variables of two arrays with a single foreach loop making a url. Some coding examples will be honored.

我有这个至今:

<?php
foreach($menu_names as $menu_name){
echo "<li><a href='?subj= " .urlencode($subjects["id"]). " '>".$menu_name."</a></li>";
}
?>

我想在这个循环中增加一个阵列

I want to add one more array within this loop

推荐答案

假设你有这两个数组相同数量的项目。

assume you have the same number of items in those two arrays.

如果你想使用的foreach(),那么阵列需要具有相同的索引:

if you want to use foreach(), then the arrays need to have the same index:

foreach ($a as $index => $value)
{
    echo $a[$index] .' '. $b[$index];
}

如果数组有数字索引,你可以使用为()

if the arrays have numeric index, you can just use for():

for ($i = 0; $i < sizeof($a); $i++)
{
    echo $a[$i] .' '. $b[$i];
}

这篇关于如何使用&QUOT多个阵列;单&QUOT; foreach()循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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