I submit to the PHP file a json array in which one of the fields is true or false. Depending on the data that the user sent to the server. And if this field is true, then when creating the array, one more element should be added. But PHP swears that it expects a character ) , and not if - PHP Parse error: syntax error, unexpected 'if' (T_IF), expecting ')' . How can this be corrected with the minimum possible amount of code without creating two conditions and the same variable in each of them?

 $post_data = array ( "signature" => $signature, "marker" => "65059", "host" => $_SERVER['SERVER_NAME'], "user_ip" => $_SERVER['REMOTE_ADDR'], "locale" => "ru", "trip_class" => ($data['is_business'] == 'true' ? 'Y' : 'C'), "passengers" => array( "adults" => $data['adults'], "children" => $data['teens'], "infants" => $data['kids'], ), "segments" => array( 0 => array( "origin" => $data['from'], "destination" => $data['to'], "date" => $data['there'], ), if($data['is_oneway'] == 'true') { 1 => array( "origin" => $data['to'], "destination" => $data['from'], "date" => $data['thence'], ), } ), ); 
  • But where did you get that you can take and write an if in the middle of an array declaration? - teran
  • after you have created your array $post_data = [ ...]; check your condition if($data['is_oneway] == 'true'){ ....} and inside it add the required data $post_data['segments][] = ['origin' => ... ] - teran

2 answers 2

 $post_data=[ 'signature' => $signature ,'marker' => 65059 ,'host' => $_SERVER['SERVER_NAME'] ,'user_ip' => $_SERVER['REMOTE_ADDR'] ,'locale' => 'ru' ,'trip_class' => ($data['is_business'] == 'true' ? 'Y' : 'C') ,'passengers' => [ 'adults' => $data['adults'] ,'children' => $data['teens'] ,'infants' => $data['kids'] ] ,'segments' => [ [ 'origin' => $data['from'] ,'destination' => $data['to'] ,'date' => $data['there'] ] ] ]; if($data['is_oneway'] == 'true') $post_data['segments'][]=[ 'origin' => $data['to'] ,'destination' => $data['from'] ,'date' => $data['thence'] ]; 

I still do not agree with the comments @Firepro

 if ($data['is_business']=='true') 

and

 if ($data['is_business']) 

not the same

you confuse it with

 if ($data['is_business']==true) 
  • one
    and would write it in the comment? - teran
  • Yes, a little hurried in the answer. Thank. A boolean value is of course better transmitted in the correct format, not a string. + - Firepro

It is forbidden to use control structures inside an array declaration.

Remarks:

1) Go to the short syntax of arrays, it is more convenient. The array () construct can be replaced by []

2) The if ($data['is_business']=='true') ) construct can be simply replaced with if ($data['is_business']) , this is more convenient, provided that you have the bool type transmitted. Transfer the boolean value from the interface in the correct format to avoid various problems with the conversion of parameters.

3) Start writing code using PSR standards, use camelCase to name variables, method names, etc.

Solution of your question:

 $postData = [ "signature" => $signature, "marker" => 65059, "host" => $_SERVER['SERVER_NAME'], "user_ip" => $_SERVER['REMOTE_ADDR'], "locale" => "ru", "trip_class" => $data['is_business'] ? 'Y' : 'C', "passengers" => [ "adults" => $data['adults'], "children" => $data['teens'], "infants" => $data['kids'], ], "segments" => [ 0 => [ "origin" => $data['from'], "destination" => $data['to'], "date" => $data['there'], ] ], ]; if ($data['is_oneway']) { $postData['segments'][] = [ "origin" => $data['to'], "destination" => $data['from'], "date" => $data['thence'], ]; } 

More information about the work of arrays in PHP, read the official site.

  • Regarding the first and third paragraph: why? I feel more comfortable as written by me. Besides, I do not think that this method will add to performance. My project is still small, but then the code will become many times larger and the code will become difficult to read. As for the boolean value, it is first transmitted as a json array (where the value is boolean), and then decoded into PHP with json_decode() and seems to be becoming a string. I'm already starting with jQuery to be confused (there, too, the Boolean value of a variable is checked in this way, but more reliable) - JamesJGoodwin
  • If a value is passed to json as {"value": bool}, during conversion it will not be a string, but a bool type, with json_decode. Well, camelCase, shorter by 1 byte: D About 3 - these are simply accepted PSR standards in PHP and a choice of many frameworks (for example, Symfony), it is desirable that everything be in the same format, less confusion. I will not argue about these points, you yourself can choose how you prefer to write, discuss it, how to argue about whether there is a chicken with your hands :) - Firepro