Php 从数据数组创建Optgroup
我正在使用Codeigniter查询我的数据库并返回一个数据数组 我得到了如下一系列数据:Php 从数据数组创建Optgroup,php,mysql,arrays,codeigniter,Php,Mysql,Arrays,Codeigniter,我正在使用Codeigniter查询我的数据库并返回一个数据数组 我得到了如下一系列数据: Array ( [0] => stdClass Object ( [depot_id] => 1 [depot_name] => Stockton On Tees [depot_description] => Arriva Stockton on Tees Depot [depot_postcode] =>
Array
(
[0] => stdClass Object
(
[depot_id] => 1
[depot_name] => Stockton On Tees
[depot_description] => Arriva Stockton on Tees Depot
[depot_postcode] => TS18 3AW
[depot_lat] =>
[depot_long] =>
[operating_company_id] => 1
[date_created] => 2014-02-14 10:24:17
[date_edited] =>
[edited_by] =>
[status] => active
[operating_company_name] => Arriva North East
[operating_company_description] => Arriva North East
[operating_company_lat] =>
[operating_company_long] =>
[operating_company_postcode] =>
[operating_group_id] => 1
)
[1] => stdClass Object
(
[depot_id] => 2
[depot_name] => Darlington
[depot_description] => Arriva Darlington Depot
[depot_postcode] => DH1 1TW
[depot_lat] =>
[depot_long] =>
[operating_company_id] => 1
[date_created] => 2014-02-14 10:24:17
[date_edited] =>
[edited_by] =>
[status] => active
[operating_company_name] => Arriva North East
[operating_company_description] => Arriva North East
[operating_company_lat] =>
[operating_company_long] =>
[operating_company_postcode] =>
[operating_group_id] => 1
)
[2] => stdClass Object
(
[depot_id] => 3
[depot_name] => Ashington
[depot_description] => Arriva Ashington Depot
[depot_postcode] => NE63 9UN
[depot_lat] =>
[depot_long] =>
[operating_company_id] => 2
[date_created] => 2014-02-14 10:46:05
[date_edited] =>
[edited_by] =>
[status] => active
[operating_company_name] => Arriva Northumbria
[operating_company_description] => Arriva Northumbria
[operating_company_lat] =>
[operating_company_long] =>
[operating_company_postcode] =>
[operating_group_id] => 1
)
[3] => stdClass Object
(
[depot_id] => 4
[depot_name] => Blyth
[depot_description] => Arriva Blyth Depot
[depot_postcode] => NE24 2AP
[depot_lat] =>
[depot_long] =>
[operating_company_id] => 2
[date_created] => 2014-02-14 10:46:05
[date_edited] =>
[edited_by] =>
[status] => active
[operating_company_name] => Arriva Northumbria
[operating_company_description] => Arriva Northumbria
[operating_company_lat] =>
[operating_company_long] =>
[operating_company_postcode] =>
[operating_group_id] => 1
)
我想基于“运营公司名称”创建一个optgroup,因此在本例中,有两个仓库位于它下面
在我看来,我目前只是使用foreach循环来创建下拉列表
<select name="depot_id" class="form-control">
<?php foreach($depots as $depot): ?>
<optgroup label="<?php echo $depot->operating_company_name; ?>">
<option value="<?php echo $depot->depot_id; ?>"><?php echo $depot->depot_name; ?></option>
</optgroup>
<?php endforeach; ?>
</select>
请尝试,首先重新格式化源阵列,如下所示:
$result = array();
foreach($depots as $depot){
$result[$depot->operating_company_name][] = $depot;
}
然后,对于创建选择try
<select name="depot_id" class="form-control">
<?php foreach($result as $key=>$val): ?>
<optgroup label="<?php echo $key; ?>">
<?php foreach($val as $option): ?>
<option value="<?php echo $option->depot_id; ?>"><?php echo $option->depot_name; ?></option>
<?php endforeach; ?>
</optgroup>
<?php endforeach; ?>
</select>
请尝试,首先重新格式化源阵列,如下所示:
$result = array();
foreach($depots as $depot){
$result[$depot->operating_company_name][] = $depot;
}
然后,对于创建选择try
<select name="depot_id" class="form-control">
<?php foreach($result as $key=>$val): ?>
<optgroup label="<?php echo $key; ?>">
<?php foreach($val as $option): ?>
<option value="<?php echo $option->depot_id; ?>"><?php echo $option->depot_name; ?></option>
<?php endforeach; ?>
</optgroup>
<?php endforeach; ?>
</select>
我想你必须使用double foreach循环来创建你想要的东西试试这个:Hi@Newbi3我在做什么double looping?在解决了这个问题之后,你会遇到一些问题。您需要循环一次,才能将操作名称放入数组中。并使用名称作为键构建数组$optgroupArray[$depot->operating_company_name]对于每一场比赛,添加depot来制作一个2D数组。我想你必须使用double foreach循环来创建你想要的。试试这个:Hi@Newbi3我在做什么?在解决了你的一些问题之后。您需要循环一次,才能将操作名称放入数组中。并使用名称作为键构建数组$optgroupArray[$depot->operating_company_name]对于每一场比赛,添加仓库以形成2D阵列。绝对超赞!谢谢你的邀请,真是太棒了!谢谢你