Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an If-else within an array

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.

like image 970
Michael Avatar asked Jul 12 '11 13:07

Michael


People also ask

Can you use if else in array?

Please format your code. You don't apply if-else in an array.

How do you write an IF condition inside an array?

For example, if condition «a» is an array of Booleans (true or false values), it returns an array with the same index, containing «b» or «c» as appropriate: Variable X := -2 .. 2. If X > 0 THEN 'Positive' ELSE IF X < 0 THEN 'Negative' ELSE 'Zero' →

How do you access an array inside another array?

To access the elements of the inner arrays, you simply use two sets of square brackets. For example, pets[1][2] accesses the 3rd element of the array inside the 2nd element of the pets array.


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

like image 172
Kokos Avatar answered Sep 27 '22 16:09

Kokos


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.

like image 44
Ostin Avatar answered Sep 27 '22 18:09

Ostin