conditionally adding a element in a array

Viewed 9601

How can I conditionally add 'b' => 'xyz' in the array below, in the array() statement?

$arr = array('a' => abc)

The ternary operator doesn't let me do it

6 Answers

This is an old question, but you can accomplish this with array_merge:

array_merge(['foo' => 'bar'], $condition ? ['baz' => 'boo' ] : []);
Related