7

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.

2
  • 1
    It is not possible to put code inside an array, but I don't see the point. An array is just a data structure, you don't need to "toggle" what it contains, just don't extract/print/whatever that part when you don't need it Commented Jul 12, 2011 at 13:42
  • 1
    For your information, PHP has a boolean type, you don't have to use strings. Commented Jul 12, 2011 at 13:42

10 Answers 10

13

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.

Sign up to request clarification or add additional context in comments.

8 Comments

How would I do this for the question now. Edited it slightly just so I understand. Thanks
You could either use this same method for the other one, or you could use one of the other simpler methods that other people are talking about. I only gave you this answer because I thought your real code was more complicated but there are far easier solutions for this situation.
Please edit your answer. I'm getting a syntax error but I can't see my mistake. Thanks
Updated, it was a simple case of copy-pasting.
Thanks. I left out the , which was creating a problem.
|
7

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.

2 Comments

Based on your example, I have proved unambiguous answer to the question! +1 Thanks!
how to avoid key 'b'
3

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

1 Comment

I edited my question slightly to better explain it. How would I use your explanation with the new question where it is controlling the inner array not the outer one. Thanks
1

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.

Comments

0

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.

Comments

0

You can do:

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

Comments

0

You could use push?

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

Comments

0

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

Comments

0

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.