如果在数组内部,则 PHP 插入速记:

Mar*_*sen 0 php if-statement

我正在制作一个带有一些必须添加到数组中的变量的函数

该数组称为 $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
    );
Run Code Online (Sandbox Code Playgroud)

如果出现以下情况,我的速记会出错:

$fields == null ? 'fields'=> $fields : ''
Run Code Online (Sandbox Code Playgroud)

我的目标是得到这样的东西:

  ($fields == null ? 'fields'=>$fields : '')
Run Code Online (Sandbox Code Playgroud)

因此,如果 $fields 为 null 或未设置索引字段甚至不应该在数组中设置

NiK*_*iZe 5

我只能看到这个问题:更改$fields == null ? 'fields' => $fields : 'gg'
'fields' => ($fields == null ? 'gg' : $fields)

编辑: 如果参数根本不应该存在,则在 $param 声明之后使用单独的语句

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

如果订单也很重要,您将需要将其拆分得更多。