嘿伙计们我是php的新手。 首先,我有一个循环,从xml文件中获取数据。 在循环内部我想创建一个if条件,它将有一个flag = 0,当$ item ['description']第二次相同时(flag = 1)然后停止并继续另一个。 任何帮助将不胜感激:D
foreach ($item_array as $item) {
$flag=0;
if ( make_safe($item['diamerisma']) == $_GET['diamerisma'] ) {
if ( $item['description'] == $item['description'] && flag == 0 ) {
$html .= '<ul data-role="listview" id="weatherList" data-theme="b" data-insert="true" >';
$html .= '<li><div id="Left1"><a href="main.php?description=Kiklades">' . make_safe( $item['description'] ) . '</a></div></li>';
$html .= '</ul>';
echo '</dd>';
$flag=1;
}
}
}
}
这是xml代码
<item>
<title>Alexan</title>
<description>Ioannina</description>
<diamerisma>Ipeiros</diamerisma>
<metar>1009</metar>
</item>
<item>
<title>Center</title>
<description>Ioannina</description>
<diamerisma>Ipeiros</diamerisma>
<metar>1010</metar>
</item>
<item>
<title>Tzoumerka</title>
<description>Ioannina</description>
<diamerisma>Ipeiros</diamerisma>
<metar>1011</metar>
</item>
<item>
<title>Center</title>
<description>Igoumenitsa</description>
<diamerisma>Ipeiros</diamerisma>
<metar>1012</metar>
</item>
<item>
<title>Paramithia</title>
<description>Igoumenitsa</description>
<diamerisma>Ipeiros</diamerisma>
<metar>1013</metar>
</item>
<item>
<title>Arta</title>
<description>Arta</description>
<diamerisma>Ipeiros</diamerisma>
<metar>1014</metar>
</item>
现在代码显示Ioannina,Ioannina,Ioannina,Igoumenitsa,Igoumenitsa,Arta和我 喜欢展示:Ioannina,Igoumenitsa,Arta
答案 0 :(得分:0)
只是一些注释:你可以跳转到循环的下一个交互作为foreach键入continue
,如果在foreach循环中设置$flag = 0
,当你检查它时它总是0。并且总是$ item ['description']将等于$ item ['description']
我的建议是在数组中使用值:
$used_values = array();
foreach ($item_array as $item) {
if ( make_safe($item['diamerisma']) == $_GET['diamerisma'] ) {
if (!in_array($item['description'], $used_values) ) {//This will check if this value hasn't been listed yet
$html .= '<ul data-role="listview" id="weatherList" data-theme="b" data-insert="true" >';
$html .= '<li><div id="Left1"><a href="main.php?description=Kiklades">' . make_safe( $item['description'] ) . '</a></div></li>';
$html .= '</ul>';
$html .= '</dd>';
$used_values[] = $item['description'];//here you preserve used values for avoiding repetition
}
}
}
答案 1 :(得分:0)
$processed=array();
foreach ($item_array as $item) {
if ( ! in_array( $item['diamerisma'], $processed ) ) //if diamerisma not in processed array
{
if ( make_safe($item['diamerisma']) == $_GET['diamerisma'] )
{
$html .= '<ul data-role="listview" id="weatherList" data-theme="b" data-insert="true" >';
$html .= '<li><div id="Left1"><a href="main.php?description=Kiklades">' . make_safe( $item['description'] ) . '</a></div></li>';
$html .= '</ul>';
echo '</dd>';
$processed[] = $item['diamerisma']; //add diamerisma to processed array
}
}
}
答案 2 :(得分:0)
看起来你想要在循环之外启动$ flat并且当它大于1时,break
退出循环。 break语句会将您从控制语句中跳出,或者更多。
$flag=0;
foreach ($item_array as $item) {
if ( make_safe($item['diamerisma']) == $_GET['diamerisma'] ) {
if ( $item['description'] == $item['description']) {
$html .= '<ul data-role="listview" id="weatherList" data-theme="b" data-insert="true" >';
$html .= '<li><div id="Left1"><a href="main.php?description=Kiklades">' . make_safe( $item['description'] ) . '</a></div></li>';
$html .= '</ul>';
echo '</dd>';
$flag += 1;
}
}
if ($flag > 1) {
break;
}
}
请参阅此处的文件: