0

How can i put "if" inside array in php? mean:

 return [
'cars' => [
   'test' => $arrayTest,
   'test1' => $arrayTest1,
],
'session' => [
   'sessionkey' => "$xyz",
   'lastlogintime' => $sess,
   if ($xyzp->available > 0){
     'availablenow' => true;
} else{
   'wait' => '7_days_waiting';
   ]
];

problem is propably in using "=>"

Thanks

3
  • You could do it with two ternary expressions: ($xyzp->available > 0 ? 'availablenow' : 'wait') => ($xyzp->available > 0 ? true : '7_days_waiting'), ... but it's a bit tortuous. Much easier to do what you want in two steps - ie initialise your array, then do your conditional expression to modify it. Commented Aug 21, 2023 at 20:17
  • You can't add a control structure inside of an array definition. Either add it separately (if you only want it to only show those keys when necessary), or use a ternary if you can set it to false as well Commented Aug 21, 2023 at 20:17
  • Consider using a ternary operator - google.com/search?q=php+ternary Commented Aug 21, 2023 at 20:17

3 Answers 3

2

You can't add control structures inside of an array assignment. You'd have to use an array for the partial result, and then add the keys conditionally afterwards.

$res = [
'cars' => [
   'test' => $arrayTest,
   'test1' => $arrayTest1,
],
'session' => [
   'sessionkey' => "$xyz",
   'lastlogintime' => $sess,
]
];

if ($xyzp->available > 0){
   $res['availablenow'] => true;
} else{
   $res['wait'] => '7_days_waiting';
}

return $res;

The other suggestions in the comments about using a ternary would work, but not necessarily cleanly since you're setting different keys based on the conditional. Using the ternary both keys would exist in the resulting array, but the "other" key would be set to NULL or another "not set" value of your choosing. Eg:

 return [
'cars' => [
   'test' => $arrayTest,
   'test1' => $arrayTest1,
],
'session' => [
   'sessionkey' => "$xyz",
   'lastlogintime' => $sess,
   'availablenow' => ($xyzp->available > 0) ? true : false,
   'wait' => ($xyzp->available > 0) ? NULL : '7_days_waiting'
]
];

Edit: lukas.j's answer has a "clean output" solution with just ternaries, but it also demonstrates the pitfall of ternaries where it becomes difficult to tell what multiple ternaries in a row are actually doing.

IMO for ternaries [and all code, really] there's a judgement call to be made between, "I want my code to be compact" and "I want my code to be easily readable".

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

1 Comment

As right as you are with the multiple ternaries in a row, you have above two ternaries in your second example which 'contradict' each other (first > 0 and then <= 0). That is not easier to process IMHO. It would probably make sense to have 'wait' also use > 0.
2

Use the ternary operator – as suggested in the other answers for the value – also for the key:

$array = [
  'cars'    => [
    'test'  => $arrayTest,
    'test1' => $arrayTest1,
  ],
  'session' => [
    'sessionkey'                                   => "$xyz",
    'lastlogintime'                                => $sess,
    $xyzp->available > 0 ? 'availablenow' : 'wait' => $xyzp->available > 0 ? true : '7_days_waiting'
  ]
];

Or:

$isAvailable = $xyzp->available > 0;

$array = [
  'cars'    => [
    'test'  => $arrayTest,
    'test1' => $arrayTest1,
  ],
  'session' => [
    'sessionkey'                           => "$xyz",
    'lastlogintime'                        => $sess,
    $isAvailable ? 'availablenow' : 'wait' => $isAvailable ? true : '7_days_waiting'
  ]
];

4 Comments

Well this would certainly work, but the next time you or anyone else looks at the code they will have to spend several minutes trying to figure out what it does and why.
Agreed with Sammitch here. Nested ternary statements are available, but generally recommended to avoid. If you must use them, it's recommended you use () to indicate nesting: $conditionOne ? 'A' : ($conditionTwo ? 'B' : 'C')
@TimLewis You are of course right about nested ternary statements, just note that but the code above does not contain any.
Haha whoops, you are correct; it was late when I read this, and I totally didn't see the => separation there 😅
1

Put the array in a variable, then use an if statement to add the conditional keys and values.

$result = [
    'cars' => [
        'test' => $arrayTest,
        'test1' => $arrayTest1,
    ],
    'session' => [
        'sessionkey' => "$xyz",
        'lastlogintime' => $sess,
    ]
];

if ($xyzp->available > 0){
     $result['session']['availablenow'] = true;
} else {
   $result['session']['wait'] = '7_days_waiting';
}
return $result;

If the key were constant and you had both an if and else value for it, you could have used a ternary in the value of that key.

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.