2

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

2
  • No, you can't have if statements inside array definitions. You can however use ternarys, though whether those are appropriate is another matter. Commented Nov 23, 2016 at 10:07
  • or you could just assign the boolean result of your comparison Commented Nov 23, 2016 at 10:09

2 Answers 2

3

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.

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

Comments

2

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]]
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.