使用数组内的if-else [英] Using an If-else within an array

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

问题描述

大家好,我不知道如果这是可能的,或者是否有这样做的另一种方式,但任何帮助,将AP preciated。我想要做的就是单独关闭阵列。所以我有这个..

Hi guys 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" => ""
        )
    )
            )
?>

如果其false,那么它也将是空白明显。它是可能的,如果再一个阵列控制另一个数组里面有一个?如果是这样它如何运作的?这仅仅是一个阵列的一部分有更多的选择和路段我只是把那些出简单性是其易于扩展,一旦我知道如何做一次。

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);

?>

它的工作原理是这样的:

It works like this:

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://$c$cpad.org/cxp0M0oL

但对于这个确切的情况还有其他的解决方案,以及

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

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

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