Home » Php » php – Using an If-else within an array

php – Using an If-else within an array

Posted by: admin July 12, 2020 Leave a comment

Questions:

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

What I want is this

<?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 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.

Thanks

How to&Answers:

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.

Answer:

Inside an array you can use ternary operator:

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

But in your example better way is to move if-statement outside your array.

Answer:

You are complicating things needlessly.

If the condition and the values you want to assign are simple enough, you can use the ternary operator (?:) like so:

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

However, this is not very readable even for simple cases and I would call it a highly questionable practice. It’s much better to keep it as simple as possible:

$condition = true;
$arrLayout = array(
    "section1" => false
);

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

Answer:

What you are suggesting is not possible. You would need to add the variables base on the if/else conditional after you have made the array.

For example:

$arrLayout = array();

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

This still rather untidy because of your array structure, I’d try eliminating some keys if you can, for example do you need section1? You could just let PHP add a numerical key by doing $arrLayout[] = array(..), which create a new ‘row’ in the array which you can still loop through.

Answer:

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" => ""
        )
    );
}
?>

Answer:

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.

Answer:

You can do:

$emptyArray = array();
$arrLayout = array("section1" => $emptyArray);

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

Answer:

You could use push?

<?php

$LibraryStatus='true'

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

Answer:

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

?>

Answer:

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