PHP foreach $输出变化?
问题描述:
如果某个变量被调用,我需要一些帮助来获得输入其他内容的规则。 要打破它,我有以下几种:
private $zebra_moto_symbol = array
("ES400", "MC9500", "MC9200", "MC9190", "MC9094", "MC9090", "MC9097", "MC9060",;
,并使用此代码它拉模型到列表页:
public function manufacturer_models_list() {
$manu_name = $this->manufacturer_name;
$output = "<ul>";
sort($this->$manu_name);
foreach($this->$manu_name as $model) {
$output .= "<li>" . "<a href=\"repair.php\">" . $model . "</a></li>";
}
$output .= "</ul>";
$output .= "<p class=\"clear\"></p>";
$output .= "<a href=\"repair.php\" " . "id=\"arrange-repair\">Arrange A Repair</a>";
return $output;
}
在所有,但其中的两个,我需要它显示repair.php链接,但是这两个需要不同。我需要输入什么才能做到这一点? 在此先感谢(对不起,这一个难倒我)。 :)
答
您可以使用switch
声明。
<?
public function manufacturer_models_list() {
$manu_name = $this->manufacturer_name;
$output = "<ul>";
sort($this->$manu_name);
foreach ($this->$manu_name as $model) {
switch($model) {
//Output NOT repair.php on this list of strings
case "ES400":
case "MC9500":
$output .= "<li>DIFFERENT OUTPUT</a></li>";
break;
//default is the action that happens if none of the previous conditions are met
default:
$output .= "<li>" . "<a href=\"repair.php\">" . $model . "</a></li>";
break;
}
}
$output .= "</ul>";
$output .= "<p class=\"clear\"></p>";
$output .= "<a href=\"repair.php\" " . "id=\"arrange-repair\">Arrange A Repair</a>";
return $output;
}
?>
了解更多关于Switch Statements
+0
这工作完美,非常感谢你:) –
答
如果我理解正确的,你想要的是有特定的值不同的输出。
我曾经想有另一个数组来保存你想要一个不同的输出值,你可以做这样的事情:
$different_output_array = ['ES400', 'MC9500']; # you can add new elements any time
,只是修改你的函数是这样的:
public function manufacturer_models_list() {
$manu_name = $this->manufacturer_name;
$output = "<ul>";
sort($this->$manu_name);
foreach($this->$manu_name as $model) {
if(in_array($model,$different_output_array))
{
$output .= "<li>" . "<a href=\"another.php\">" . $model . "</a></li>";
}
else
{
$output .= "<li>" . "<a href=\"repair.php\">" . $model . "</a></li>";
}
}
$output .= "</ul>";
$output .= "<p class=\"clear\"></p>";
$output .= "<a href=\"repair.php\" " . "id=\"arrange-repair\">Arrange A Repair</a>";
return $output;
}
希望这可以帮助。
你可以格式化你的代码吗(如果可能的话)在它目前的形式是有点难以阅读。 – Neal
欢迎来到[so]。花点时间探索问题编辑器的网站和功能。有一个不错的''''按钮,可用于将文本块标记为代码。另外,请勿将所有代码放在一行中,因为它很难阅读。在编辑器下,您可以预览您的问题以了解它的外观,并在提交前进行调整。 – axiac