简体   繁体   English

PHP插入简写如果在数组内:

[英]Php insert shorthand if inside array:

im making a function with some variables which has to be added to an array 我用一些变量将函数添加到数组

the array is called $params this is my attempt however im getting erros: 该数组称为$ params,这是我的尝试,但是即时通讯越来越出错:

     $params = array(
        'Target' => 'Report'
    ,'Method' => 'getStats',
     $fields == null ? 'fields' => $fields : 'gg'
    ,'filters' => array(
            'Stat.affiliate_id' => array(
                'conditional' => 'EQUAL_TO'
            ,'values' => array( $affId ))
        ,'Stat.date' => array (
                'conditional' => 'BETWEEN'
            ,'values' => array(
                    $startDate
                ,$endDate
                )
            )
        )
    ,$ascSorting
        //group by day
    ,'groups' => array(
            $grouping_type)
    ,'totals' => true
    );

im getting an error at my shorthand if: 即时通讯在我的速记上出现错误,如果:

$fields == null ? 'fields'=> $fields : ''

My goal is to get something like this: 我的目标是得到这样的东西:

  ($fields == null ? 'fields'=>$fields : '')

So if $fields is null or not set the index fields should not even be set in the array 因此,如果$ fields为null或未设置,则甚至不应在数组中设置索引字段

I can only see this issue: Change $fields == null ? 'fields' => $fields : 'gg' 我只能看到此问题:Change $fields == null ? 'fields' => $fields : 'gg' $fields == null ? 'fields' => $fields : 'gg' to $fields == null ? 'fields' => $fields : 'gg'
'fields' => ($fields == null ? 'gg' : $fields)

Edit: If the parameter should not be there at all then use a separate statement after the $param declaration 编辑:如果参数根本不存在,则在$ param声明后使用单独的语句

if ($fields != null) $param['fields'] = $fields;

If the order is important as well you will need to split it up even more. 如果订单也很重要,您将需要进一步拆分。

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

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