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.
Please format your code. You don't apply if-else in 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' →
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With