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'], ), } ), );
ifin the middle of an array declaration? - teran$post_data = [ ...];check your conditionif($data['is_oneway] == 'true'){ ....}and inside it add the required data$post_data['segments][] = ['origin' => ... ]- teran