PHP if elseif else 基于数组

发布于 2024-12-14 08:10:28 字数 921 浏览 1 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深海蓝天 2024-12-21 08:10:28

您对数组进行寻址的方式不正确。我假设该数组将被称为 $data

或者用 var_dump(...) 上的任何内容替换 $data

然后,您的代码将如下所示:

if ($data['radioinput'] == "sidebar-left"){
   echo '<body class="sidebar-left">';
}elseif ($data['radioinput'] == "sidebar-right"){
   echo '<body class="sidebar-right">';
}else{
   //otherwise
}

编辑:您甚至可以将其简化为:

if ($data['radioinput'] == "sidebar-right"){
   echo '<body class="sidebar-right">';
}else{
    echo '<body class="sidebar-left">';
}

干杯:)

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 on var_dump(...)

Then, you code would look like:

if ($data['radioinput'] == "sidebar-left"){
   echo '<body class="sidebar-left">';
}elseif ($data['radioinput'] == "sidebar-right"){
   echo '<body class="sidebar-right">';
}else{
   //otherwise
}

Edit: You can even simplify that down to:

if ($data['radioinput'] == "sidebar-right"){
   echo '<body class="sidebar-right">';
}else{
    echo '<body class="sidebar-left">';
}

Cheers :)

殊姿 2024-12-21 08:10:28

改变这个:

if (radioinput('sidebar-left')) { 

到这个:

// This assumes you named the array as $array, this was not mentioned in OP
if ($array['radioinput'] == 'sidebar-left') { 

Change this:

if (radioinput('sidebar-left')) { 

To this:

// This assumes you named the array as $array, this was not mentioned in OP
if ($array['radioinput'] == 'sidebar-left') { 
冰雪梦之恋 2024-12-21 08:10:28
$class = $array['radioinput'] == 'sidebar-right' ? 'right' : 'left';
printf('<body class="sidebar-%s">', $class);
$class = $array['radioinput'] == 'sidebar-right' ? 'right' : 'left';
printf('<body class="sidebar-%s">', $class);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文