同步迭代并打印两个相同大小数组中的值
我想使用两个数组生成一个选择框,一个包含国家/地区代码,另一个包含国家/地区名称。
这是一个例子:
$codes = ['tn', 'us', 'fr'];
$names = ['Tunisia', 'United States', 'France'];
foreach( $codes as $code and $names as $name ) {
echo '<option value="' . $code . '">' . $name . '</option>';
}
这个方法对我不起作用。有什么建议吗?
I want to generate a selectbox
using two arrays, one containing the country codes and another containing the country names.
This is an example:
$codes = ['tn', 'us', 'fr'];
$names = ['Tunisia', 'United States', 'France'];
foreach( $codes as $code and $names as $name ) {
echo '<option value="' . $code . '">' . $name . '</option>';
}
This method didn't work for me. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(26)
那是无效的。
您可能想要这样的东西...
或者,将代码作为
$names
数组的键会更容易...That is not valid.
You probably want something like this...
Alternatively, it'd be much easier to make the codes the key of your
$names
array...foreach
一次仅对一个数组进行操作。按照数组的结构方式,您可以将它们
array_combine()
放入键值对数组中,然后foreach
该单个数组:或者如其他答案中所示,您可以改为硬编码关联数组。
foreach
operates on only one array at a time.The way your array is structured, you can
array_combine()
them into an array of key-value pairs thenforeach
that single array:Or as seen in the other answers, you can hardcode an associative array instead.
使用
array_combine()
融合数组一起并迭代结果。Use
array_combine()
to fuse the arrays together and iterate over the result.array_map 似乎对此也有好处
其他好处是:
如果一个数组比另一个数组短,则回调会接收
null
值来填充间隙。您可以使用 2 个以上的数组进行迭代。
array_map seems good for this too
Other benefits are:
If one array is shorter than the other, the callback receive
null
values to fill in the gap.You can use more than 2 arrays to iterate through.
使用关联数组:
我相信使用关联数组是最明智的方法,而不是使用 array_combine() ,因为一旦有了关联数组,您就可以简单地使用 array_keys()< /code> 或
array_values()
以获得与之前完全相同的数组。Use an associative array:
I believe that using an associative array is the most sensible approach as opposed to using
array_combine()
because once you have an associative array, you can simply usearray_keys()
orarray_values()
to get exactly the same array you had before.这对我有用:
This worked for me:
像这样的代码是不正确的,因为 foreach 仅适用于单个数组:
替代方案,更改为:
Your code like this is incorrect as foreach only for single array:
Alternative, Change to this:
走出去......
PHP 5.3+
PHP 5.3 之前
或合并
in select
演示
Walk it out...
PHP 5.3+
Before PHP 5.3
or combine
in select
demo
为什么不直接合并到一个
多维关联数组中?看起来你正在做这个错误:变成:
Why not just consolidate into a
multi-dimensionalassociative array? Seems like you are going about this wrong:becomes:
所有经过充分测试的
从数组创建动态下拉列表的 3 种方法。
这将从数组创建一个下拉菜单并自动分配其各自的值。
方法#1(普通数组)
方法#2(普通数组)
方法#3(关联数组)
All fully tested
3 ways to create a dynamic dropdown from an array.
This will create a dropdown menu from an array and automatically assign its respective value.
Method #1 (Normal Array)
Method #2 (Normal Array)
Method #3 (Associative Array)
您可以使用 array_merge 组合两个数组,然后迭代它们。
You can use array_merge to combine two arrays and then iterate over them.
array_combine()
对我来说非常有用,同时组合来自多个表单输入的$_POST
多个值以尝试更新购物车中的产品数量。array_combine()
worked great for me while combining$_POST
multiple values from multiple form inputs in an attempt to update products quantities in a shopping cart.foreach 仅适用于单个数组。要单步执行多个数组,最好在 while 循环中使用each() 函数:
each() 返回有关数组当前键和值的信息,并将内部指针加一,如果达到了则返回 false数组的末尾。该代码不依赖于具有相同键或具有相同类型元素的两个数组。当两个数组之一完成时循环终止。
foreach only works with a single array. To step through multiple arrays, it's better to use the each() function in a while loop:
each() returns information about the current key and value of the array and increments the internal pointer by one, or returns false if it has reached the end of the array. This code would not be dependent upon the two arrays having identical keys or having the same sort of elements. The loop terminates when one of the two arrays is finished.
不要尝试 foreach 循环(仅当数组具有相同长度时)。
Instead of foreach loop, try this (only when your arrays have same length).
我举了一个简单的例子
i gave a simple example
我通过这种方式解决了像你这样的问题:
I solved a problem like yours by this way:
您应该尝试将 2 个数组放入 singler foreach 循环中
假设我有 2 个数组
1.$item_nm
2.$item_qty
You should try this for the putting 2 array in singlr foreach loop
Suppose i have 2 Array
1.$item_nm
2.$item_qty
我认为你可以这样做:
$codes = array('tn','us','fr');
$names = array('突尼斯','美国','法国');
它也应该适用于关联数组。
I think that you can do something like:
$codes = array('tn','us','fr');
$names = array('Tunisia','United States','France');
It should also work for associative arrays.
我认为最简单的方法就是这样使用 for 循环:
I think the simplest way is just to use the for loop this way:
En Laravel Livewire
En laravel Livewire
现在你可以对每个数组使用 $a ....
Now you can use $a for each array....
少数数组也可以这样迭代:
Few arrays can also be iterated like this:
仅当两个数组具有相同的计数时,这才有效。我尝试在 laravel 中,将两个数组插入 mysql db
$answer = {"0":"0","1":"1","2":"0 ","3":"0","4":"1"};
$reason_id = {"0":"17","1":"19","2":"15","3":"19","4":"18"};
This will only work if the both array have same count.I try in laravel, for inserting both array in mysql db
$answer = {"0":"0","1":"1","2":"0","3":"0","4":"1"};
$reason_id = {"0":"17","1":"19","2":"15","3":"19","4":"18"};
为了避免通过索引引用值以及通过
array_combine()
使用一个数组的值作为键来避免数据损坏的风险,您可以使用array_map()
在同一时间。代码:(演示)
内爆一个由选项标签填充的数组,callabck 签名中没有参数:
通过使用扩展运算符累加传入的值来内爆一个由选项填充的数组:
转置两个或多个数组,然后迭代并打印二维数组中的行数据:
使用 printf() 系列函数可以避免串联和插值。创建包含引号的字符串时,这可以帮助您的代码更具可读性。
To avoid referencing values by index and risking data corruption by using one array's values as keys via
array_combine()
, you can usearray_map()
to iterate two or more arrays at the same time.Codes: (Demo)
Implode an array populated with options tags without parameters in callabck signature:
Implode an array populated with options by accumulating passed-in values with the spread operator:
Transpose two or more arrays, then iterate and print the row data from the 2d array:
Using
printf()
family functions voids concatenation and interpolation. When creating strings containing quotes, this can help your code to be more readable.这对我有用
it works for me