Using if-else in an array

Hi guys, I have no idea if this is possible or if there is another way to do this, but any help would be appreciated. What I'm trying to do is disconnect arrays individually. So I have it ..

<?php $arrLayout = array( "section1" => array( "wLibrary" => array( "title" => "XBMC Library", "display" => "" ), "wControl" => array( "title" => "Control", "display" => "" ) ) ) ?> 

I want 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 empty. Is it possible to have if if inside an array controlling another array? If so, how will it work? This is just part of the array, there are more options and sections. I just took them for simplicity, since they are easy to scale, as soon as I understand how to do it once.

thanks

+6
source share
10 answers

Yes, it is possible using a specific 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 as follows:

 if($a == $b){ echo 'a'; }else{ echo 'b'; } 

equally

 echo $a == $b ? 'a' : 'b'; 

If you use this shorthand, it will always return a result, so you can put it between brackets and put it between arrays.

http://codepad.org/cxp0M0oL

But there are other solutions for this exact situation.

+10
source

Inside the array, you can use the ternary operator:

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

But in your example, it is best to move the if-statement outside of your array.

+5
source

You uselessly complicate things.

If the condition and the values ​​you want to assign are simple enough, you can use the ternary operator ( ?: As follows:

 $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 very dubious practice. It is much better to simplify it as much as possible:

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

What you offer is impossible. You will need to add a variable base in the if / else condition after you make the array.

For instance:

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

This is still pretty untidy due to your array structure, I would try to remove some keys if possible, for example, do you need section1 ? You could just let PHP add the numeric key by doing $arrLayout[] = array(..) , which creates a new "string" in the array, which you can still skip.

+1
source

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 .

0
source

You can do:

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

Can you use push?

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

In a way, yes.

You cannot place it where you requested (immediately after opening the array) You cannot use the if statement. Can you use a triple (condition)? true: false

 <?php $LibraryStatus = 'true'; $array = array( "section1" => ($LibraryStatus == 'true') ? array("wLibrary" => array("title" => "Title","display" => "")) : array() ); ?> 
0
source

Another way is to include logic in either the function or through the 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), ) ?> 

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

This issue was detected when setting up PDO debugging mode, which depends on configuration settings.

The examples above were large, but a bit ambiguous, so I decided to write another simple example of how to do this:

 array( 'key' => $variable ? 'Sets certain value if $variable === true' : 'Sets certain value if $variable === false' ); 
0
source

Source: https://habr.com/ru/post/892575/


All Articles