我有以下数组,其中包含一个数组:
Array
(
[title] => SwB Skipper
[today] => 08/11/2016
[crew_name] => Array
(
[0] => Array
(
[name] => Bob S
)
[1] => Array
(
[name] => Janet
)
[2] => Array
(
[name] => Perry S
)
[3] => Array
(
[name] => Vinay N
)
[4] => Array
(
[name] => Pace W
)
这个数组叫做$values;
我做一个:
extract($values);
然后尝试用
访问$crew_name['name']
元素<?php foreach ($crew_name['name'] as $crew): ?>
<option value = "<?php echo $crew['name']; ?>" >
<?php echo $crew['name']; ?> </option>
<?php endforeach ?>
您还没有准备好在$crew_name
的顶层使用['name']
键。$crew_name
只有数字键,所以你只需要
<?php foreach ($crew_name as $crew): ?>
假设$values是主数组试试:
foreach($values as $val){
foreach($val["crew_name"] as $crew){
echo $crew["name"];
}
}
您可以简单地使用aray_column()函数并使您的多数组成为紧凑的单维数组。这会使事情简单得多。
试试这个:
<select>
<?php
$options = array_column($values["crew_name"], "name");
foreach($options as $option) {
?>
<option value = "<?php echo $option; ?>" >
<?php echo $option; ?>
</option>
<?php } ?>
</select>