PHP if elseif else 基于数组
我的 var_dump 返回这个:
array(5) {
["radioinput"]=> string(12) "sidebar-left"
["option1"]=> int(0)
["sometext"]=> string(0) ""
["selectinput"]=> NULL
["sometextarea"]=> string(0) ""
}
我在处理“radioinput”数组时遇到问题。
如果它是“sidebar-left”,我希望它回显:
如果它是“sidebar-right”,我希望它回显:
< body class="sidebar-left">
如果它是“two-sidebars”,我希望它回显:
如果它是空白,我想它回显:
我的问题是,如何让我的代码来执行此操作?
<?php
if (radioinput('sidebar-left')) {
echo '<body class="sidebar-left">';
} elseif (radioinput('sidebar-right')) {
echo '<body class="sidebar-right">';
} elseif (radioinput('two-sidebars')) {
echo '<body class="two-sidebars">';
} else {
echo '<body class="sidebar-left">';
}
?>
My var_dump is returning this:
array(5) {
["radioinput"]=> string(12) "sidebar-left"
["option1"]=> int(0)
["sometext"]=> string(0) ""
["selectinput"]=> NULL
["sometextarea"]=> string(0) ""
}
I'm having problems acting on the "radioinput" array.
If it's "sidebar-left" I want it to echo:
<body class="sidebar-left">
If it's "sidebar-right" I want it to echo:
<body class="sidebar-left">
If it's "two-sidebars" I want it to echo:
<body class="two-sidebars">
If it's blank I want it to echo:
<body class="sidebar-left">
My questions is, how can I get my code to do this?
<?php
if (radioinput('sidebar-left')) {
echo '<body class="sidebar-left">';
} elseif (radioinput('sidebar-right')) {
echo '<body class="sidebar-right">';
} elseif (radioinput('two-sidebars')) {
echo '<body class="two-sidebars">';
} else {
echo '<body class="sidebar-left">';
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您对数组进行寻址的方式不正确。我假设该数组将被称为
$data
。或者用
var_dump(...)
上的任何内容替换$data
然后,您的代码将如下所示:
编辑:您甚至可以将其简化为:
干杯:)
The way you are addressing your array isn't correct. I am assuming that the array is going to be called
$data
.or replace
$data
with whatever you had onvar_dump(...)
Then, you code would look like:
Edit: You can even simplify that down to:
Cheers :)
改变这个:
到这个:
Change this:
To this: