array(...)构造内的条件元素 [英] A conditional element inside an array(...) construct
问题描述
我的系统将配置数组发送给类似这样的函数:
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)?
赞
callThatFunction ( array(
k1 => v1,
if($cond( {k2 => v2,}
...
kn=vn));
以上显然在语法上是错误的,但应该表达我的想法.
The above is obviously wrong syntactically, but should express my idea.
谢谢
推荐答案
最终我想到了以下内容:
Eventually I came up with the following:
callThatFunction( 数组(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
这篇关于array(...)构造内的条件元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!