0

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

2
  • Please provide the error, and also a example of how the final array should look. Commented Aug 29, 2013 at 13:22
  • @NiKiZe its my shorthand if that is the problem within the array! the i want to check if the variable $field is set Commented Aug 29, 2013 at 13:23

1 Answer 1

6

I can only see this issue: Change $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.

Sign up to request clarification or add additional context in comments.

5 Comments

This would work but my idea is that if $fields is not there shouldnt even be a field index (i know that my question states that it should be set to gg but actually it shouldnt be set at all)
so something like this: ($fields == null ? 'fields'=>$fields : '')
shorthand if statements only return a value, and can not be used as you want it to, you can only have the if around the key or the value, not around both in one if. Would it be a solution if you changed the key 'fields' to say 'nofields' if it is null?
it would but the API i am using is very sensitive ... :S
Well then you will have to split up generating the array over atleast 2 statements, and if the order is important even more so, (but I recomment you test first with only using keys with if as described in my edit.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.