简体   繁体   English

如果/ Else三元运营商发布空白其他声明

[英]If/Else Ternary Operator with Blank Else Statement Issue

Usually I can get away with using something like: 通常我可以使用以下内容:

$a = ($condition)? array(1,2,3) : '';

But now I have a method that accepts a multidimensional array and I must pass or not pass one of the arrays conditionally. 但是现在我有一个接受多维数组的方法,我必须有条件地传递或不传递其中一个数组。

$this->mymethod(
    'myarrays'=> array(
        array('key' => 'lang_code'),
        array('key' => 'lang_description'),
        array('key' => 'lang_direction'),
        ($mycondition==true)? array('key' => 'lang_export') : ),
    )
);

Basically, the issue is with the last array passed. 基本上,问题是传递的最后一个数组。 And more specifically the ELSE statement in the ternary If operator. 更具体地说,三元If运算符中的ELSE语句。 It seems that I can't pass simply a blank space after the : and I can't pass anything else like FALSE or '' (empty string), because later on in the code the foreach that runs through this array gives errors. 似乎我不能在以下之后简单地传递空格:而且我无法传递任何其他内容,如FALSE或''(空字符串),因为稍后在代码中,运行此数组的foreach会产生错误。

My question is: How to pass a parameter to a function/method based on a condition? 我的问题是: 如何根据条件将参数传递给函数/方法?

Use: 采用:

$mycondition? array('key' => 'lang_export') : null

Now, you could simply run it through array_filter(..) , to remove that NULL element. 现在,您只需通过array_filter(..)运行它,即可删除该NULL元素。

array_filter(array(
    array('key' => 'lang_code'),
    array('key' => 'lang_description'),
    array('key' => 'lang_direction'),
    $mycondition ? array('key' => 'lang_export') : null),
));

This will remove the null 这将删除null

Aim for readability, not for how can you type less. 瞄准可读性,而不是如何减少打字。 The ternary operator is great because in certain cases it increases readability. 三元运算符很棒,因为在某些情况下它会提高可读性。 This is certainly not that case. 当然不是这种情况。

Be nice to the people reading your code later, including yourself. 对以后阅读代码的人们要好,包括你自己。

Here is an example (comments are the thoughts of the future reader): 这是一个例子(评论是未来读者的想法):

//OK, so here we have an array
$array = array(
    array('key' => 'lang_code'),
    array('key' => 'lang_description'),
    array('key' => 'lang_direction'),
);

//So the array can have one more element based on this condition
if ($mycondition) {
     $array[] = array('key' => 'lang_export');
}

//And then we pass this array to the method
$this->mymethod(array('myarrays' => $array));

You are free to use variables and don't have to write all your code in one statement (well, I must admit I thought that was cool earlier). 你可以自由地使用变量,而不必在一个语句中写下你所有的代码(好吧,我必须承认我之前认为这很酷)。

You could evaluate the condition before the function call, eg with a helper-function: 您可以在函数调用之前评估条件,例如使用辅助函数:

function evaluate_condition (array $mandatory, array $optional, $condition) {
    if (true === $condition) {
        $mandatory[] = $optional;
    }
    return $mandatory;
}

$this->mymethod(
    'myarrays'=> $this->evaluate_condition(
        array(
            array('key' => 'lang_code'),
            array('key' => 'lang_description'),
            array('key' => 'lang_direction')
        ),
        array('key' => 'lang_export'),
        $mycondition
    )
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM