简体   繁体   中英

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 = 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

I can only see this issue: Change $fields == null ? 'fields' => $fields : 'gg' $fields == null ? 'fields' => $fields : 'gg' to
'fields' => ($fields == null ? 'gg' : $fields)

Edit: If the parameter should not be there at all then use a separate statement after the $param declaration

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

If the order is important as well you will need to split it up even more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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