今天是个好日子。
我想为工具类别创建一个下拉菜单,并删除显示在“类别”标签中的所有重复项。
到目前为止,我有:
<select id="tool" name="category">
<?php
$tools = simplexml_load_file('tools.xml');
$categoryArray = array();
foreach($tools as $tool) {
if(!in_array((string)$tool->category, $categoryArray)) {
$categoryArray[] = (string)$tool->category;
}
}
print_r("<option value='".$categoryArray."'>".$categoryArray."</option>");
?>
</select>
但是,当我运行此代码时,类别不会出现,而是在下拉列表中仅显示“ Array”文本。
我该如何解决这个问题?
供参考,这是我的tools.xml的样子:
<tools>
<tool type="..." web-based="..." free="...">
<name>...</name>
<description>...</description>
<url>...</url>
<subjects>...</subjects>
<creators>...</creators>
<category>Office tool</category>
<price>...</price>
</tool>
</tools>
目前,您尝试<option>
在创建所有类别的数组之后尝试打印元素。而是在添加新类别时将其包括在循环中...
foreach($tools as $tool) {
if(!in_array((string)$tool->category, $categoryArray)) {
$categoryArray[] = (string)$tool->category;
print_r("<option value='".(string)$tool->category."'>".(string)$tool->category."</option>");
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句