Using an If-else within an array

后端 未结 10 1902
滥情空心
滥情空心 2020-12-11 04:45

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.

相关标签:
10条回答
  • 2020-12-11 05:06

    Encountered this problem, when was setting up PDO debug mode, that is dependent on config settings.

    Examples above were great but a bit ambiguous, so I decided to write another, simple example of how to do it:

    array(
        'key' => $variable ? 'Sets certain value if $variable === true' : 'Sets certain value if $variable === false'
    );
    
    0 讨论(0)
  • 2020-12-11 05:10

    In a way, yes.

    You can't place it where you've asked (directly after the opening of an array) You can't use an if statement. You can use ternary (condition) ? true : false

    <?php
    
    $LibraryStatus = 'true';
    
    $array = array(
        "section1" => ($LibraryStatus == 'true') ? array("wLibrary" => array("title" =>     "Title","display" => "")) : array()  
    );
    
    ?>
    
    0 讨论(0)
  • 2020-12-11 05:12

    No, you cannot have an if-else block in the middle of an array declaration. You can, however, manipulate the array in different ways to achieve the desired result. See array functions.

    0 讨论(0)
  • 2020-12-11 05:13

    Another way is to include the logic in either a function or via an include file.

    With function:

    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" => ""
            )
        );
    }
    ?>
    
    0 讨论(0)
  • 2020-12-11 05:15

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

    is equal to

    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.

    0 讨论(0)
  • 2020-12-11 05:19

    You can do:

    $emptyArray = array();
    $arrLayout = array("section1" => $emptyArray);
    
    $LibraryStatus= true ;
    if ($LibraryStatus=== true) {
         $arrLayout["section1"]["wlibrary"] =  array("title" => "XBMC Library","display" => "" );
    }
    
    0 讨论(0)
提交回复
热议问题