0

I have an multidimensional array, and i want to add if condition in array. Like :

$check = "failed";

$data = array(
    "personalizations" => array(
        array(
            "to" => array(
                array("email" => $userEmail, "name" => $userName)
            ),
            if(strpos($check, "SUCCESS") !== false){

            }else{
                "CC" => array(
                    array("email" => $adminEmail,"name" => $adminEmail)
                ) 
            } 
        )
    ),
    "from" => array("email" => $senderEmail, "name" => $senderName),
    "subject" => $subject,
    "content" => array(array("type" => "text/html", "value" => $body))
);

I want to send an email to the administrator. If the $subject does not have the word success, add CC array to the array, otherwise do not add cc.

I am getting error : syntax error, unexpected 'if' (T_IF), expecting ')'

0

1 Answer 1

2

You can't put PHP control structures in the middle of defining an array like that. First create the array with the default values, then put the if-statement after and push new elements to the array, if needed.

Something like this:

$data = array(
    "personalizations" => array(
        array(
            "to" => array(
                array("email" => $userEmail, "name" => $userName)
            ),
        )
    ),
    "from" => array("email" => $senderEmail, "name" => $senderName),
    "subject" => $subject,
    "content" => array(array("type" => "text/html", "value" => $body))
);

// Now add the condition
if(strpos($check, "SUCCESS") === false){
    $data['personalizations'][0]["CC"] = array(
        array("email" => $adminEmail,"name" => $adminEmail)
    ) 
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.