在数组中使用 If-else [英] Using an If-else within an array

查看:37
本文介绍了在数组中使用 If-else的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是否可行,或者是否有其他方法可以做到,但任何帮助将不胜感激.我想要做的是单独关闭阵列.所以我有这个:

I have no idea if this is possible or if there is another way of doing it but any help would be appreciated. What I'm trying to do is turn off arrays individually. So I have this:

<?php
$arrLayout = array(
    "section1" => array(

        "wLibrary" => array(
            "title" => "XBMC Library",
            "display" => ""
        ),

        "wControl" => array(
            "title" => "Control",
            "display" => ""
        )
        )
            )
?>

我想要的是这个

<?php

$LibraryStatus='true'

$arrLayout = array(
    "section1" => array(

                  if $LibraryStatus='true' (

        "wLibrary" => array(
            "title" => "XBMC Library",
            "display" => ""
        ),
                  else blank.      

              if $ControlStatus='true' (

    "wControl" => array(
            "title" => "Control",
            "display" => ""
        )
    )
            )
?>

如果它是假的,那么它显然也将是空白的.是否可以在控制另一个数组的数组中使用 if then ?如果是这样,它将如何工作?这只是数组的一部分,还有更多的选项和部分,为了简单起见,我将它们去掉了,因为一旦我理解如何做一次,它就可以轻松扩展.

If its false then it will also be blank obviously. Is it possible to have an if then inside an array controlling another array? If so how would it work? This is just part of the array there are more options and sections I just took those out for simplicity as its easy to scale once I understand how to do it once.

推荐答案

是的,使用某种速记可以做到:

Yes, this is possible using a certain shorthand:

<?php

$LibraryStatus = $ControlStatus = true;

$arrLayout = array(
             "section1" => array(
             ($LibraryStatus ? array("wLibrary" => array("title"   => "XMBC Library",
                                                         "display" => "")) : false),
             ($ControlStatus ? array("wControl" => array("title"   => "Control",
                                                         "display" => "")) : false)));

print_r($arrLayout);

?>

它是这样工作的:

if($a == $b){ echo 'a'; }else{ echo 'b'; }

等于

echo $a == $b ? 'a' : 'b';

如果你使用这个简写,它总是会返回输出,所以你可以把它放在括号之间,然后放在数组之间.

If you use this shorthand it will always return the output, so you can put it between brackets and put it inbetween the array.

http://codepad.org/cxp0M0oL

但对于这种确切情况,还有其他解决方案.

But for this exact situation there are other solutions as well.

这篇关于在数组中使用 If-else的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆