Conditionally add element inside an array(...) [duplicate]
Asked Answered
D

6

25

My system sends a configuration array to a function like this:

callThatFunction( array(k1 => v1,  k2 => v2, ... kn=vn));

I want to make one of the key value pairs, conditional upon some circumstances.

Can I do this without creating a variable for the array (and this breaking the clean config syntax that someone else had created)?

Like this

callThatFunction (
    array(
        k1 => v1, 
        if($cond( {k2 => v2,} 
        ... 
        kn=vn
    )
);

The above is obviously wrong syntactically, but should express my idea.

Doi answered 7/11, 2010 at 17:44 Comment(0)
D
8

Eventually I came up with the following:

callThatFunction( array(k1 => v1, k2 => v2, ... kn=vn) + ($cond ? array(key=>value) : array()) )

Will still appreciate a suggestion for somethings that will express the intention more directly

Doi answered 7/11, 2010 at 18:20 Comment(1)
This is quite elegant, especially with php 5.4+ square bracket array notation. Eg. ($cond ? [ key=>value ] : [])Metic
M
50

You can use the conditional operator cond ? true-expr : false-expr:

$someConfig = array(
    'k1' => 'v1',
    'k2' => $cond ? 'v2a' : 'v2b'
);

The conditional expression $cond ? 'v2a' : 'v2b' will yield 'v2a' if $cond evaluates to true and 'v2b' otherwise. But this works only with the value of a key.

If you only want to add a key based on a condition, you need to use a separate if:

$someConfig = array('k1' => 'v1');
if ($cond) {
    $someConfig['k2'] = 'v2';
}

Edit    You can add keys conditionally without a variable using the array union operator or array_merge:

array('k1' => 'v1') + ($cond ? array('k2' => 'v2') : array())
array_merge(array('k1' => 'v1'), $cond ? array('k2' => 'v2') : array())

Now you need to decide what’s more readable or better to maintain.

Mok answered 7/11, 2010 at 17:48 Comment(1)
Thanks, aware of this, I edited my question to stress the following: currently there is no name to the array. Conditioning only the value is not what I need - I need the pair to not exist if cond is falseDoi
A
11

You can assign all values and filter empty keys from the array at once like this:

$myArray = array_filter([
    'k1' => 'v1',
    'k2' => $cond ? 'v2' : false
]);

This allows you avoid the conditional after the fact and imo it's fairly readable.

UPDATE: Sometimes you may wish to preserve falsey values which would otherwise get thrown out with the above method. You can be a bit more specific with the callback to discard only null values for instance like this:

$myArray = array_filter([
    'k1' => 'v1',
    'k2' => $cond ? 'v2' : null,
    'k3' => 0,
    'k4' => false,
], fn($v) => $v !== null);

The above would preserve k3 and k4 but conditionally discard k2.

Avocet answered 26/8, 2016 at 15:49 Comment(1)
I use this, however i wonder, is there any traversal shorthand. Think when you are multiple layers deep but need to evaluate a condition based on a one level parent value, it a lot of extra typing to be like ($val['level1']['level2']['level3'] == 'x') ? 'this' : 'that' - it would be slick to be like (this.parent == 'x') ? 'this' : 'that'Isidora
D
8

Eventually I came up with the following:

callThatFunction( array(k1 => v1, k2 => v2, ... kn=vn) + ($cond ? array(key=>value) : array()) )

Will still appreciate a suggestion for somethings that will express the intention more directly

Doi answered 7/11, 2010 at 18:20 Comment(1)
This is quite elegant, especially with php 5.4+ square bracket array notation. Eg. ($cond ? [ key=>value ] : [])Metic
M
6

Put it after the array declaration:

$someConfig = array(
...
);

if($cond){ $someConfig['k2'] = $v2; }
Morbidezza answered 7/11, 2010 at 17:48 Comment(1)
Thanks, I edited my question to stress the following: currently there is no name to the arrayDoi
J
2

I think all of these answers so far will leave a stub of some kind in the array when the condition is not met.

I suggest setting the array in full, then use the unset function to remove those you don't want. This will give a clean array with only what you want and is also easy to read.

So...

$shapes = array('one'=>'circle', 'three'=>'triangle', 'four'=>'square', 'five'=>'pentagon')
if($i_like_curves==false){
   unset($shapes['one']);
}
Jointed answered 14/8, 2014 at 4:55 Comment(0)
T
0

The "splat" operator ... can be used in the case of numeric index arrays, I.e: we need ["v1","v2","v3"] or ["v1","v3"], depends on the condition $cond.

[
...
array_merge([
'v1'
],
($cond ? ['v2']:[])
),
'v3'
]

Note: if $cond is false, 'v3' will get the index '1', as it gives you

Array ( [0] => v1, [1] => v3 )

Triatomic answered 14/6, 2023 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.