php array(...) 结构中的条件元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4118875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:02:25  来源:igfitidea点击:

A conditional element inside an array(...) construct

php

提问by shealtiel

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.

以上在语法上显然是错误的,但应该表达我的想法。

Thank you

谢谢

采纳答案by shealtiel

Eventually I came up with the following:

最终我想出了以下几点:

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

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

仍然会欣赏对更直接表达意图的建议

回答by Gumbo

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

您可以使用条件运算符cond ? true-expr : false-expr

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

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

如果计算结果为,则条件表达式$cond ? 'v2a' : 'v2b'将产生结果。但这仅适用于键的值。'v2a'$cond'v2b'

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

如果只想根据条件添加键,则需要使用单独的if:

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


Edit????You can add keys conditionally without a variable using the array union operatoror array_merge:

编辑????您可以使用数组联合运算符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.

现在您需要决定哪些内容更具可读性或更好地维护。

回答by GWW

Put it after the array declaration:

把它放在数组声明之后:

$someConfig = array(
...
);

if($cond){ $someConfig['k2'] = $v2; }

回答by billynoah

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.

这使您可以避免事后的条件,并且它具有相当的可读性。

回答by Paul Vincent Hewson

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.

我建议设置完整的数组,然后使用 unset 函数删除那些你不想要的。这将提供一个干净的数组,其中只包含您想要的内容并且易于阅读。

So...

所以...

$shapes = array('one'=>'circle, 'three'=>'triangle, 'four'=>'square', 'five'=>'pentagon')
if($i_like_curves==false){
   unset($shapes['one']);
}