php - 如何在对象中一次循环遍历 2 个或多个数组
问题描述
我有一个包含两个数组的 php 对象,我需要同时遍历这两个数组并显示选择选项。table->list
应该在值内,并且应该tables->title
在选项 HTML 输出内。
这是我的代码:
$tables = new \stdClass();
$tables->list = ['bmg_contact_us','bmg_volunteer'];
$tables->title = ['Contact us', 'Volunteer'];
<select name="bmg-forms" onchange="submission_frm.submit();">
<?php
foreach ($tables as $key => $table) {
echo "<option value='" . $tables->list . "'>'" . $tables->title . "'</option>";
}
?>
</select>
解决方案
$tables = new \stdClass();
$tables->list = ['bmg_contact_us','bmg_volunteer'];
$tables->title = ['Contact us', 'Volunteer'];
$options = array_combine($tables->list,$tables->title);//both array count must be same
输出:
Array
(
[bmg_contact_us] => Contact us
[bmg_volunteer] => Volunteer
)
html:
<select name="bmg-forms" onchange="submission_frm.submit();">
<?php
foreach ($options as $key => $value) {
echo "<option value='" . $key . "'>'" . $value . "'</option>";
}
?>
</select>
推荐阅读
- json - 确定 Type 是否为纯“数据类型”
- python - python请求库:从附加路径获取响应的函数
- r - 如何更改 RDA 箭头的字体和字体大小 - ggvegan?
- microsoft-graph-api - 通过 REST API 读取 MS Teams 中的聊天线程
- php - 将 WooCommerce 功能的特定结果打印为简码
- python-3.x - 无法使用 .json 密钥文件创建 Pub/Sub Publisher Client
- python - 在文件中写入嵌入列表
- r - 使用 R Markdown 和内置 Microsoft 主题生成 PowerPoint 幻灯片
- postgresql - chown: /var/lib/postgresql/data/postgresql.conf: 只读文件系统
- jquery - jquery convert text to image