Reputation: 4296
I have an array each array has a value.
Here is an example of this:
$sales_payload = array(
'organization_id' => $organization_id,
'contact_id' => $contact_id,
'status' => 'Open',
'subject' => $_product->post_title." ".str_replace($strToRemove, "", $_POST['billing_myfield12']),
'start_date' => date("Y-m-d"), // set start date on today
'expected_closing_date' => date("Y-m-d",strtotime(date("Y-m-d")."+ 14 days")), // set expected closing date 2 weeks from now
'chance_to_score' => '10%',
'expected_revenue' => 0, //set the expected revenue
'note' => $_POST['order_comments'],
'progress' => array(
'id'=>'salesprogress:200a53bf6d2bbbfe' //fill a valid salesprogress id to set proper sales progress
),
"custom_fields"=> [[
if(strpos($_POST['billing_myfield13'], 'ja') !== false) { [["actief_in_duitsland"=>1]] }
]]
);
I am now trying to fill a certain array in my code this one:
"custom_fields"=> [["actief_in_duitsland"=>1]]
This works. Only thing is I want the value to be =>1 on a certain condition.
This condition is if a certain POST request contains a certain string then make the value => 1
I tried this :
"custom_fields"=> [[
if(strpos($_POST['billing_myfield13'], 'ja') !== false) { [["actief_in_duitsland"=>1]] }
]]
So
if(strpos($_POST['billing_myfield13'], 'ja') !== false) { [["actief_in_duitsland"=>1]] }
If the $_POST['billing_myfield13']
contains the word 'ja'
then [["actief_in_duitsland"=>1]]
Upvotes: 2
Views: 6348
Reputation: 1674
If statement inside the array shouldn't work, you can try following code
$actief_in_duitsland = (strpos($_POST['billing_myfield13'], 'ja') !== false) ? 1 : 0;
$sales_payload = array(
'organization_id' => $organization_id,
'contact_id' => $contact_id,
'status' => 'Open',
'subject' => $_product->post_title." ".str_replace($strToRemove, "", $_POST['billing_myfield12']),
'start_date' => date("Y-m-d"), // set start date on today
'expected_closing_date' => date("Y-m-d",strtotime(date("Y-m-d")."+ 14 days")), // set expected closing date 2 weeks from now
'chance_to_score' => '10%',
'expected_revenue' => 0, //set the expected revenue
'note' => $_POST['order_comments'],
'progress' => array(
'id'=>'salesprogress:200a53bf6d2bbbfe' //fill a valid salesprogress id to set proper sales progress
),
"custom_fields"=> [['actief_in_duitsland' => $actief_in_duitsland]]
);
Upvotes: 2
Reputation: 4246
You can use ternary conditions, which is a shortcut for an if
statement. Here is an example of how ternary conditions works :
A regular if would be written like this :
if( "mycondition" == 1 )
{
$boolean = true;
}
else {
$boolean = false;
}
The equivalent of this statement with ternary condition would be written like below :
$boolean = ( "mycondition" == 1 ) ? true : false;
You might want to use this shortcut to instanciate your array like following :
$sales_payload = [
// ...
'custom_fields' => ( strpos($_POST['billing_myfield13'], 'ja') !== false ) ? [['actief_duitsland' => 1]] : [['actief_duitsland' => '???']],
// ...
];
Warning
You should also define a else
value for this statement.
Upvotes: 3