PHP Ternary statement within Associative Array to set both key & value?

12,178

All you're doing with that ternary operator is to decide on the key name. The element is going to be put into the array no matter what, it's right there in the array literal declaration.

The best you could do with an inline expression is something like this:

['foo' => 'bar'] + (true ? [] : ['baz' => 42])

Practically, you should probably better write:

$arr = ['foo' => 'bar'];
if (true) {
    $arr['baz'] = 42;
}

Writing more compact code is not always the best goal to have, it needs to be readable and understandable first of all.

Share:
12,178
thatnetworkguy
Author by

thatnetworkguy

Updated on June 05, 2022

Comments

  • thatnetworkguy
    thatnetworkguy almost 2 years

    I've been searching and testing for some time and just can't find if what I am trying to accomplish is possible the way I'm going about it.

    I would like to add a key/value pair to an array, when defining the array, based on an ternary statement.

    Array(
        'Item 1' => 'Some value',
        (FALSE)? NULL : 'Item 2' => 'Another Value'
    )
    

    My expected/wanted behavior is for the result to then be:

    Array (
      [Item 1] => Some value
      [Item 2] => Another Value
    )
    

    And when the statement is True:

    Array(
        'Item 1' => 'Some value',
        (TRUE)? NULL : 'Item 2' => 'Another Value'
    )
    

    Yield:

    Array (
      [Item 1] => Some value
    )
    

    But instead I get:

    Array (
      [Item 1] => Some value
      [] => Another Value
    )
    

    Which causes problems since A) I don't want this second element to exist in the first place and B) The False condition value is assigned to the True conditions value (in this case NULL, which sets the key to NULL [])

    This is very peculiar. Is my only option to have a standard if(){} statement and Pop the element if the condition is false (by using !) into the array?

    Note: Having a null value for Item 2 is not acceptable, rather if the initial condition is true, no element should exist at all.

    Any help would be greatly appreciated!

  • imkingdavid
    imkingdavid over 9 years
    Because this is tagged under php-5.3 it might be better not to use the short array syntax that is only available in 5.4+. But the answer is still correct.
  • thatnetworkguy
    thatnetworkguy about 9 years
    Thank you, I went with the second method and the code still reads cleanly!
  • hakre
    hakre about 9 years
    The "no matter what" is the key-point here. You can make it fail: stackoverflow.com/a/29327671/367456 :)
  • Austin
    Austin about 9 years
    That's actually pretty amazing, even if you should never ever do this.
  • Gromski
    Gromski over 6 years
    @MáximaAlekz If you’re insinuating that the unreadable version is faster: it’s not. Secondarily, both Google and Facebook are heavily investing in their own languages and/or compilers; they’re solving the performance issue at the compiler level, not the code level (up to a point, obviously). Further, given the enormous team sizes and code bases in both companies, I’m sure maintainability, which includes readability, matters extremely to them.