php 循环两个数组的值_php - foreach循环中的两个数组

php - foreach循环中的两个数组

我想使用两个数组生成selectbox,一个包含国家/地区代码,另一个包含国家/地区名称。

这是一个例子:

$codes = array('tn','us','fr');

$names = array('Tunisia','United States','France');

foreach( $codes as $code and $names as $name ) {

echo '' . $name . '';

}

?>

这种方法对我不起作用。 有什么建议?

medk asked 2019-04-23T19:46:33Z

22个解决方案

129 votes

foreach( $codes as $code and $names as $name ) { }

那是无效的。

你可能想要这样的东西......

foreach( $codes as $index => $code ) {

echo '' . $names[$index] . '';

}

或者,使代码成为您的$names阵列的关键要容易得多......

$names = array(

'tn' => 'Tunisia',

'us' => 'United States',

...

);

alex answered 2019-04-23T19:46:55Z

73 votes

array_combine()一次只能在一个阵列上运行。

你的数组结构的方式,你可以array_combine()他们成一个键值对数组然后foreach那个单个数组:

foreach (array_combine($codes, $names) as $code => $name) {

echo '' . $name . '';

}

或者如其他答案所示,您可以改为对关联数组进行硬编码。

BoltClock answered 2019-04-23T19:47:34Z

22 votes

使用array_combine()将阵列融合在一起并迭代结果。

$countries = array_combine($codes, $names);

Ignacio Vazquez-Abrams answered 2019-04-23T19:47:59Z

6 votes

使用关联数组:

$code_names = array(

'tn' => 'Tunisia',

'us' => 'United States',

'fr' => 'France');

foreach($code_names as $code => $name) {

//...

}

我相信使用关联数组是最明智的方法,而不是使用array_combine(),因为一旦你有一个关联数组,你可以简单地使用array_keys()或array_values()来获得你以前完全相同的数组。

Jacob Relkin answered 2019-04-23T19:48:31Z

3 votes

为什么不只是整合成一个多维关联数组? 好像你正在犯这个错误:

$codes = array('tn','us','fr');

$names = array('Tunisia','United States','France');

变为:

$dropdown = array('tn' => 'Tunisia', 'us' => 'United States', 'fr' => 'France');

Jakub answered 2019-04-23T19:48:58Z

3 votes

全部经过全面测试

3种从数组创建动态下拉列表的方法。

这将从数组创建一个下拉菜单,并自动分配其各自的值。

方法#1(正常数组)

$names = array('tn'=>'Tunisia','us'=>'United States','fr'=>'France');

echo '';

foreach($names AS $let=>$word){

echo ''.$word.'';

}

echo '';

?>

方法#2(正常数组)

$countries = array('tn'=> "Tunisia", "us"=>'United States',"fr"=>'France');

foreach($countries as $select=>$country_name){

echo '' . $country_name . '';

}

?>

方法#3(关联数组)

$my_array = array(

'tn' => 'Tunisia',

'us' => 'United States',

'fr' => 'France'

);

echo '';

echo 'Select...';

foreach ($my_array as $k => $v) {

echo '' . $v . '';

}

echo '';

?>

Funk Forty Niner answered 2019-04-23T19:49:55Z

2 votes

您可以使用array_merge组合两个数组,然后迭代它们。

$array1 = array("foo" => "bar");

$array2 = array("hello" => "world");

$both_arrays = array_merge((array)$array1, (array)$array2);

print_r($both_arrays);

Haider Ali answered 2019-04-23T19:50:38Z

2 votes

走出去......

$codes = array('tn','us','fr');

$names = array('Tunisia','United States','France');

PHP 5.3+

array_walk(array_combine($codes,$names), function ($name,$code){

@$opts = '' . $name . '';

})

echo "$opts";

在PHP 5.3之前

array_walk(array_combine($codes,$names), function ($name,$code){

@$opts = '' . $name . '';

})

echo "$opts";

或结合

array_walk(array_combine($codes,$names), function ($name,$code){

@$opts = '' . $name . '';

})

echo "$opts";

在选择

array_walk(array_combine($codes,$names), function ($name,$code){

@$opts = '' . $name . '';

})

echo "$opts";

演示

oLinkWebDevelopment answered 2019-04-23T19:51:45Z

2 votes

$codes = array ('tn','us','fr');

$names = array ('Tunisia','United States','France');

echo '

foreach(array_keys($codes) as $i) {

echo '

';

echo ($i + 1);

echo '

';

echo $codes[$i];

echo '

';

echo $names[$i];

echo '

';

}

echo '

';

?>

r5d answered 2019-04-23T19:52:04Z

2 votes

而不是foreach循环,尝试这个(仅当您的数组具有相同的长度时)。

$number = COUNT($_POST["codes "]);//count how many arrays available

if($number > 0)

{

for($i=0; $i

{

$codes =$_POST['codes'][$i];

$names =$_POST['names'][$i];

//ur code in here

}

}

fchan answered 2019-04-23T19:52:29Z

2 votes

这对我有用:

$codes = array('tn', 'us', 'fr');

$names = array('Tunisia', 'United States', 'France');

foreach($codes as $key => $value) {

echo "Code is: " . $codes[$key] . " - " . "and Name: " . $names[$key] . "
";

}

delAmux answered 2019-04-23T19:52:50Z

1 votes

我认为你可以这样做:

$ codes = array('tn','us','fr');

$ names = array('突尼斯','美国','法国');

foreach ($codes as $key => $code) {

echo '' . $names[$key] . '';

}

它也适用于关联数组。

pashri answered 2019-04-23T19:53:35Z

1 votes

我认为最简单的方法就是以这种方式使用for循环:

$codes = array('tn','us','fr');

$names = array('Tunisia','United States','France');

for($i = 0; $i < sizeof($codes); $i++){

echo '' . $names[$i] . '';

}

SeReGa answered 2019-04-23T19:54:01Z

1 votes

foreach仅适用于单个数组。 要逐步执行多个数组,最好在while循环中使用each()函数:

while(($code = each($codes)) && ($name = each($names))) {

echo '' . $name['value'] . '';

}

each()返回有关当前键和数组值的信息,并将内部指针递增1,如果已到达数组末尾,则返回false。 此代码不依赖于具有相同键或具有相同类型元素的两个数组。 当两个阵列中的一个完成时,循环终止。

PerceptorII answered 2019-04-23T19:54:33Z

0 votes

array_combine()对我来说非常有用,同时结合了$_POST来自多个表单输入的多个值,以尝试更新购物车中的产品数量。

Calin Rusu answered 2019-04-23T19:54:59Z

0 votes

if(isset($_POST['doors'])=== true){

$doors = $_POST['doors'];

}else{$doors = 0;}

if(isset($_POST['windows'])=== true){

$windows = $_POST['windows'];

}else{$windows = 0;}

foreach($doors as $a => $b){

现在你可以为每个数组使用$ a ....

$doors[$a]

$windows[$a]

....

}

Gvice answered 2019-04-23T19:55:25Z

0 votes

我通过这种方式解决了像你这样的问题:

foreach(array_keys($idarr) as $i) {

echo "Student ID: ".$idarr[$i]."
";

echo "Present: ".$presentarr[$i]."
";

echo "Reason: ".$reasonarr[$i]."
";

echo "Mark: ".$markarr[$i]."
";

}

Ulugov answered 2019-04-23T19:55:51Z

0 votes

您应该在singlr foreach循环中尝试使用put 2数组假设我有2个阵列1.$ item_nm2.$ item_qty

`<?php $i=1; ?>

Sr.Noitem_nmitem_qty

@foreach (array_combine($item_nm, $item_qty) as $item_nm => $item_qty)

$i++ $item_nm $item_qty

@endforeach `

JADAV AKASH answered 2019-04-23T19:56:18Z

0 votes

很少有数组也可以这样迭代:

foreach($array1 as $key=>$val){ // Loop though one array

$val2 = $array2[$key]; // Get the values from the other arrays

$val3 = $array3[$key];

$result[] = array( //Save result in third array

'id' => $val,

'quant' => $val2,

'name' => $val3,

);

}

Тарас Костюк answered 2019-04-23T19:56:44Z

0 votes

像这样的代码不正确,因为foreach仅适用于单个数组:

$codes = array('tn','us','fr');

$names = array('Tunisia','United States','France');

foreach( $codes as $code and $names as $name ) {

echo '' . $name . '';

}

?>

替代方案,改为:

$codes = array('tn','us','fr');

$names = array('Tunisia','United States','France');

$count = 0;

foreach($codes as $code) {

echo '' . $names[count] . '';

$count++;

}

?>

Nao Desu answered 2019-04-23T19:57:16Z

-1 votes

这个对我有用

$counter = 0;

foreach($codes as $code)

{

$codes_array[$counter]=$code;

$counter++;

}

$counter = 0;

foreach($names as $name)

{

echo $codes_array[$counter]."and".$name;

$counter++;

}

mcjarod answered 2019-04-23T19:57:37Z

-2 votes

如果可以按照您的喜好进行操作,那么2个数组的foreach将按如下方式编程:

//foreach ($array1 as $key1 [,/and/~/'/-/whatever] $array2 as $key2)

//equals:

$array1 = array();

$array2 = array();

if (sizeof(array1) == sizeof(array2)){

for ($i = 0; i < sizeof(array1) , i++){

$key1 = $array1[$i]

$key2 = $array2[$i]

//the code

}}

我的解决方案并不完美,但对大多数用途来说都没问题。

Jannis answered 2019-04-23T19:58:09Z

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值