phparraysconditional-statementsassociative-arrayconditional-operator

Conditionally add associative element to array


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.


Solution

  • $a = array('a' => 'abc') + ($condition ? array('b' => 'xyz') : array());