使用递归数组迭代器的多维数组值,用于选择选项组



我有这个新的多维数组,我正在尝试使用选项组进行多个下拉选择。以便每个选择都包含带有"$value[ 'proj_id' ]$value[ 'projName' ]的选项。选择按 ID 分组(第一个选择:127,第二个选择:131,第三个选择:130 等(选项按客户端名称分组,首先选择组另一个客户端,具有一个选项。第二选择组尼斯有两个选项等。在我使用RecursiveArrayIterator()之前,现在它不再有效:

$iterator = new RecursiveArrayIterator( $project_data_group );
$dropdown = '';
// get  all children for dropdown
while ( $iterator->valid() ) {
if ( $iterator->hasChildren() ) {
foreach ( $iterator->getChildren() as $key1 => $value ) {
$dropdown .= "<option data-proj_id=" . $value[ 'proj_id' ] . " value=" . $value[ 'projName' ] . ">" . $value[ 'projName' ] . "</option>";
}
}
break;
}
array (
127 => 
array (
'Another client' => 
array (
0 => 
array (
'dateTime' => '2020-06-04 09:33:00',
'id' => '127',
'cID' => '50',
'client' => 'Another client',
'proj_id' => '103',
'projName' => 'Logo new',
'is_from_main_table' => '1',
),
),
'Nice' => 
array (
0 => 
array (
'dateTime' => '2020-06-04 09:33:00',
'id' => '127',
'cID' => '47',
'client' => 'Nice',
'proj_id' => '106',
'projName' => 'Advert',
'is_from_main_table' => '0',
),
1 => 
array (
'dateTime' => '2020-06-04 09:33:00',
'id' => '127',
'cID' => '47',
'client' => 'Nice',
'proj_id' => '102',
'projName' => 'Webpage',
'is_from_main_table' => '0',
),
),
'Mikrosoft' => 
array (
0 => 
array (
'dateTime' => '2020-06-04 09:33:00',
'id' => '127',
'cID' => '51',
'client' => 'Mikrosoft',
'proj_id' => '104',
'projName' => 'Poster',
'is_from_main_table' => '0',
),
),
'Gooble' => 
array (
0 => 
array (
'dateTime' => '2020-06-04 09:33:00',
'id' => '127',
'cID' => '48',
'client' => 'Gooble',
'proj_id' => '105',
'projName' => 'Poster another',
'is_from_main_table' => '0',
),
),
),
131 => 
array (
'Nice' => 
array (
0 => 
array (
'dateTime' => '2020-06-08 17:23:00',
'id' => '131',
'cID' => '47',
'client' => 'Nice',
'proj_id' => '102',
'projName' => 'Webpage',
'is_from_main_table' => '1',
),
1 => 
array (
'dateTime' => '2020-06-08 17:23:00',
'id' => '131',
'cID' => '47',
'client' => 'Nice',
'proj_id' => '106',
'projName' => 'Advert',
'is_from_main_table' => '0',
),
),
'Another client' => 
array (
0 => 
array (
'dateTime' => '2020-06-08 17:23:00',
'id' => '131',
'cID' => '50',
'client' => 'Another client',
'proj_id' => '103',
'projName' => 'Logo new',
'is_from_main_table' => '0',
),
),
'Mikrosoft' => 
array (
0 => 
array (
'dateTime' => '2020-06-08 17:23:00',
'id' => '131',
'cID' => '51',
'client' => 'Mikrosoft',
'proj_id' => '104',
'projName' => 'Poster',
'is_from_main_table' => '0',
),
),
'Gooble' => 
array (
0 => 
array (
'dateTime' => '2020-06-08 17:23:00',
'id' => '131',
'cID' => '48',
'client' => 'Gooble',
'proj_id' => '105',
'projName' => 'Poster another',
'is_from_main_table' => '0',
),
),
),
130 => 
array (
'Gooble' => 
array (
0 => 
array (
'dateTime' => '2020-06-09 17:30:00',
'id' => '130',
'cID' => '48',
'client' => 'Gooble',
'proj_id' => '105',
'projName' => 'Poster another',
'is_from_main_table' => '1',
),
),
'Nice' => 
array (
0 => 
array (
'dateTime' => '2020-06-09 17:30:00',
'id' => '130',
'cID' => '47',
'client' => 'Nice',
'proj_id' => '106',
'projName' => 'Advert',
'is_from_main_table' => '0',
),
1 => 
array (
'dateTime' => '2020-06-09 17:30:00',
'id' => '130',
'cID' => '47',
'client' => 'Nice',
'proj_id' => '102',
'projName' => 'Webpage',
'is_from_main_table' => '0',
),
),
'Another client' => 
array (
0 => 
array (
'dateTime' => '2020-06-09 17:30:00',
'id' => '130',
'cID' => '50',
'client' => 'Another client',
'proj_id' => '103',
'projName' => 'Logo new',
'is_from_main_table' => '0',
),
),
'Mikrosoft' => 
array (
0 => 
array (
'dateTime' => '2020-06-09 17:30:00',
'id' => '130',
'cID' => '51',
'client' => 'Mikrosoft',
'proj_id' => '104',
'projName' => 'Poster',
'is_from_main_table' => '0',
),
),
),
133 => 
array (
'Nice' => 
array (
0 => 
array (
'dateTime' => '2020-06-11 19:58:00',
'id' => '133',
'cID' => '47',
'client' => 'Nice',
'proj_id' => '106',
'projName' => 'Advert',
'is_from_main_table' => '1',
),
1 => 
array (
'dateTime' => '2020-06-11 19:58:00',
'id' => '133',
'cID' => '47',
'client' => 'Nice',
'proj_id' => '102',
'projName' => 'Webpage',
'is_from_main_table' => '0',
),
),
'Another client' => 
array (
0 => 
array (
'dateTime' => '2020-06-11 19:58:00',
'id' => '133',
'cID' => '50',
'client' => 'Another client',
'proj_id' => '103',
'projName' => 'Logo new',
'is_from_main_table' => '0',
),
),
'Mikrosoft' => 
array (
0 => 
array (
'dateTime' => '2020-06-11 19:58:00',
'id' => '133',
'cID' => '51',
'client' => 'Mikrosoft',
'proj_id' => '104',
'projName' => 'Poster',
'is_from_main_table' => '0',
),
),
'Gooble' => 
array (
0 => 
array (
'dateTime' => '2020-06-11 19:58:00',
'id' => '133',
'cID' => '48',
'client' => 'Gooble',
'proj_id' => '105',
'projName' => 'Poster another',
'is_from_main_table' => '0',
),
),
),
134 => 
array (
'Mikrosoft' => 
array (
0 => 
array (
'dateTime' => '2020-06-22 13:36:00',
'id' => '134',
'cID' => '51',
'client' => 'Mikrosoft',
'proj_id' => '104',
'projName' => 'Poster',
'is_from_main_table' => '1',
),
),
'Nice' => 
array (
0 => 
array (
'dateTime' => '2020-06-22 13:36:00',
'id' => '134',
'cID' => '47',
'client' => 'Nice',
'proj_id' => '106',
'projName' => 'Advert',
'is_from_main_table' => '0',
),
1 => 
array (
'dateTime' => '2020-06-22 13:36:00',
'id' => '134',
'cID' => '47',
'client' => 'Nice',
'proj_id' => '102',
'projName' => 'Webpage',
'is_from_main_table' => '0',
),
),
'Another client' => 
array (
0 => 
array (
'dateTime' => '2020-06-22 13:36:00',
'id' => '134',
'cID' => '50',
'client' => 'Another client',
'proj_id' => '103',
'projName' => 'Logo new',
'is_from_main_table' => '0',
),
),
'Gooble' => 
array (
0 => 
array (
'dateTime' => '2020-06-22 13:36:00',
'id' => '134',
'cID' => '48',
'client' => 'Gooble',
'proj_id' => '105',
'projName' => 'Poster another',
'is_from_main_table' => '0',
),
),
),
)

我还没有测试过这个,但根据你想要的分组,这种方法可能会奏效。

function getGroupedDropdownOptions($array) {
$options = [];
foreach ( $array as $groups ) {
foreach ( $groups as $clientName => $group ) {
// You should be able to use $clientName or $group['client'] here
$options[$group['client']][] = ["proj_name" => $group['projName'],  "proj_id" => $group['proj_id'];
}
break;
}
return $options;
}

使用 getGroupedDropdownOptions(( 设置 $dropdownOptions 后,像这样构建 HTML。

<select>
<?php foreach($dropdownOptions as $groupName => $group): ?>
<optgroup label="<?php echo $groupName; ?>">
<?php foreach($group as $option): ?>
<option data-proj_id="<?php echo $option['proj_id']; ?>" value="<?php echo $option['proj_name']; ?>"> <?php echo $option['proj_name']; ?></option>
<?php endforeach; ?>
</optgroup>
<?php endforeach; ?>
</select>

最新更新