1

我想提取与组织相对应的所有值作为组织选项在下拉菜单下,但现在只有组织的最后一个值作为选项显示在下拉菜单中这是我的代码

<?php
$items = json_decode('[{"location":[{"building":   ["Building1"],"name":"Location1"}],"name":"Organization1"},{"location":[{"building":["Building2"],"name":"location2"}],"name":"Organisation2"},{"location":[{"building":["Building3"],"name":"Location3"}],"name":"Organization3"}]');
foreach( $items as $each ){
echo $each->location[0]->building[0];
echo $each->location[0]->name;
echo $each->name;
}
$org=$each->name;
$arr=array($org);
reset($arr);
//print_r($org); 
//$result = count($org);
//echo $result;
while(list(,$value)=each($arr)){
//echo "value:$value<br/>\n";
//$_SESSION['organisation']=$value;
//echo $_SESSION['organisation'];

}?>
<select name="category_id">
<option value=""></option>
<?php
$keys = array_keys($arr);
$count1=count($keys);
echo $count1;
for($i=0; $i<count($arr); $i++)
{?>
<option value="<?php echo $keys[$i]; ?>"><?php echo $arr[$i]; ?></option>
<?php
}
?>
</select>
4

2 回答 2

1

利用第一个 foreach 而不是开始一个全新的循环。经过测试,这有效:

<?php
    $items = json_decode('[{"location":[{"building":   ["Building1"],"name":"Location1"}],"name":"Organization1"},{"location":[{"building":["Building2"],"name":"location2"}],"name":"Organisation2"},{"location":[{"building":["Building3"],"name":"Location3"}],"name":"Organization3"}]');
    echo '<select name="category_id"><option value=""></option>';
    $stepper = 0;
    foreach($items as $each) {
        $building = $each->location[0]->building[0];
        $name = $each->location[0]->name;
        $final_name = $each->name;
        echo '<option value="'.$stepper.'">'.$final_name.'</option>';
        $stepper++;
    }
    echo '</select>';
?>
于 2013-03-23T04:25:48.700 回答
0

使用此代码

<?php
$org = array();
$items = json_decode('[{"location":[{"building":   ["Building1"],"name":"Location1"}],"name":"Organization1"},{"location":[{"building":["Building2"],"name":"location2"}],"name":"Organisation2"},{"location":[{"building":["Building3"],"name":"Location3"}],"name":"Organization3"}]');
foreach( $items as $each ){
$org[]=$each->name;
}
?>
<select name="category_id">
<option value=""></option>
<?php
foreach($org as $key=>$val)
{?>
<option value="<?php echo $key; ?>"><?php echo $val; ?></option>
<?php
}
?>
</select>
于 2013-03-23T04:25:28.907 回答