WagnerMatosUK
WagnerMatosUK

Reputation: 4429

How to use if/else inside an array

Basically I have a relatively complex piece of code in which has several arrays nested inside each other. I believe that what I'd like to do is not strictly possible:

$selector = true,
 $main_array = array(
  'general' = array(
   'another_array' = array(
    'option1' = array(
     'title' = 'Some Title',
     'type' = 'select',
       if ($selector == true) {
    'option2' = array(
     'title' = 'Some title',
     'type' = 'select',
     } else
    ...

I know that the above is not possible. I know that I could use ternary operator but it didn't work when I tried. I know that there better ways to solve this problem, but as far I can tell, all of them would involve me changing the whole structure (as there are many arrays nested inside each other) which I don't want to do, so my question is, is there a way around that I could use to make this conditional work without me changing the whole thing?

Many thanks

Upvotes: 1

Views: 206

Answers (4)

Gilbert
Gilbert

Reputation: 3776

Well, if you heart is set on using if/else rather than a ternary you can work some of the results out a bit early.

       if ($selector == true) {
    $preset2 = array(
     'title' = 'Some title',
     'type' = 'select'
     );
    }
    else
    {
     $preset2 = array( ... );
    }

 $main_array = array(
  'general' = array(
   'another_array' = array(
    'option1' = array(
     'title' = 'Some Title',
     'type' = 'select' ),
    'option2' = $preset2,

Upvotes: 0

femtoRgon
femtoRgon

Reputation: 33341

I'm assuming that you are not actually attempting to assign arrays to string literals, so judiciously adding some '>'s

And yes, if I understand what you are looking for, it can be accomplished with a ternary operation, as follows:

$main_array = array(
 'general' => array(
  'another_array' => array(
   'option1' => array(
    'title' => 'Some Title',
    'type' => 'select',
   'option2' => (($selector == true) ?
    array(
     'title' => 'Some title',
     'type' => 'select',
    ) : 'It was false'
 )))));

Upvotes: 3

unsunghero
unsunghero

Reputation: 1001

Use the ternary operator. With the ternary operator, this:

if($selector == true){
    $foo = 'something'; 
} else {
    $foo = 'something else';
}

can become this:

$foo = ($selector == true) ? 'something' : 'something else';

Your example code would look something like this:

$selector = true;    
$main_array = array(
    'general' => array(
         'another_array' => array(
              'option1' => array(
                    'title' => 'Some Title',
                    'type' => 'select',
                    'option2' => ($selector == true) ? array('title' = 'Some title','type' = 'select') : array('title' => 'Some other title', 'type' => 'Some other type')
                ...

Upvotes: 0

user428517
user428517

Reputation: 4193

You're right: what you want to do isn't possible the way you want to do it. You'll have to restructure your code. You could push each element into the array individually, using if statements where necessary to determine what gets pushed.

Upvotes: 0

Related Questions