在数组中使用If-else语句


Using an If-else within an array

我不知道这是否可能,或者是否有另一种方法,但任何帮助都将不胜感激。我要做的是逐个关闭数组。这里是:

<?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然后在数组内控制另一个数组?如果是这样,它将如何运作?这只是数组的一部分还有更多的选项和section我只是为了简单起见把它们去掉了因为一旦我知道怎么做就很容易缩放了

是的,这可以使用某种简写:

<?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';

如果您使用这种简写,它将始终返回输出,因此您可以将其放在括号之间并将其放在数组之间。

http://codepad.org/cxp0M0oL

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

在数组内部可以使用三元操作符:

$a = array(
    'b' => $expression == true ? 'myWord' : '';
);

但是在你的例子中,更好的方法是将if语句移出你的数组。

你把事情复杂化了。

如果条件和要赋值的值足够简单,可以使用三元操作符(?:),如下所示:

$condition = true;
$arrLayout = array(
    "section1" => $condition ?
                     array(
                         "wLibrary" => array(
                             "title" => "XBMC Library",
                             "display" => ""
                         )
                     ) : false,
)

然而,即使在简单的情况下,这也不是很好读,我认为这是一个非常值得怀疑的做法。越简单越好:

$condition = true;
$arrLayout = array(
    "section1" => false
);
if($condition) {
    $arrLayout["section1"] = array(
                                  "wLibrary" => array(
                                     "title" => "XBMC Library",
                                     "display" => ""
                                  )
                             );
}

你的建议是不可能的。在创建数组之后,您需要根据if/else条件添加变量。

例如:

$arrLayout = array();
if($LibraryStatus) {
    $arrLayout['section1'] = array("wLibrary" => array(
            "title" => "XBMC Library",
            "display" => ""
        ));
}

这仍然相当不整洁,因为你的数组结构,我会试着消除一些键,如果你可以,例如,你需要section1 ?你可以让PHP通过$arrLayout[] = array(..)添加一个数字键,它在数组中创建一个新的"行",你仍然可以循环。

不可以,不能在数组声明中间使用if-else块。但是,您可以用不同的方式操作该数组以获得所需的结果。

你可以这样做:

$emptyArray = array();
$arrLayout = array("section1" => $emptyArray);
$LibraryStatus= true ;
if ($LibraryStatus=== true) {
     $arrLayout["section1"]["wlibrary"] =  array("title" => "XBMC Library","display" => "" );
}

你可以使用push?

<?php
$LibraryStatus='true'
$arrLayout = array();
if ($LibraryStatus=='true') {
push($arrLayout["section1"], array(
        "wLibrary" => array(
            "title" => "XBMC Library",
            "display" => ""
        ));
}
?>

在某种程度上,是的。

你不能把它放在你要求的地方(直接在数组的开头)你不能使用if语句。你可以使用ternary (condition) ?True: false

<?php
$LibraryStatus = 'true';
$array = array(
    "section1" => ($LibraryStatus == 'true') ? array("wLibrary" => array("title" =>     "Title","display" => "")) : array()  
);
?>

另一种方法是将逻辑包含在函数中或通过include文件包含。

与功能:

function section1Function($status = false){
    if ($status){
        return array(
            "wLibrary" => array(
                "title" => "XBMC Library",
                "display" => ""
            )
        );
    } else {
        return array(
            "wControl" => array(
                "title" => "Control",
                "display" => ""
            )
        );
    }
}
$LibraryStatus='true'
$arrLayout = array(
    "section1" => section1Function($LibraryStatus),
)
?>

With include file:

<?php

$LibraryStatus='true'
$arrLayout = array(
    "section1" => require( dirname(__FILE__) .'/section1Layout.php'),
)
?>

section1Layout.php:

<?php
if ($LibraryStatus){
    return array(
        "wLibrary" => array(
            "title" => "XBMC Library",
            "display" => ""
        )
    );
} else {
    return array(
        "wControl" => array(
            "title" => "Control",
            "display" => ""
        )
    );
}
?>

遇到这个问题,当设置PDO调试模式时,这取决于配置设置。

上面的例子很好,但有点模棱两可,所以我决定再写一个简单的例子来说明如何做:

array(
    'key' => $variable ? 'Sets certain value if $variable === true' : 'Sets certain value if $variable === false'
);