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.
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'
);
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()
);
?>
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.
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" => ""
)
);
}
?>
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.
You can do:
$emptyArray = array();
$arrLayout = array("section1" => $emptyArray);
$LibraryStatus= true ;
if ($LibraryStatus=== true) {
$arrLayout["section1"]["wlibrary"] = array("title" => "XBMC Library","display" => "" );
}