Function that verifies a text and replaces the words NEED HELP please :(

Thats the controller

public function test($id, $language) {

	$this->LoadModel('Email');
	$emailWithoutValues = $this->Email->getContentTemplate($id, $language);
	
	
	

	$values = array("first_name"=>"Fredi","password"=>"test","discount_code"=>"DISC20","lala"=>"asdasd");
	$mailWithValues = $this->Email->prepareContent($emailWithoutValues, $values);
	
   
	
	$emailName = $this->Email->find('first',array('conditions' => array('Email.email_id' => $id),'fields' => array('email_name')));
    $withEmail = str_replace('{{email}}',$emailName['Email']['email_name'],$emailWithoutValues);

	
	
	
	//print_r ($mailWithValues);
    print_r ($withEmail);
	//print_r ($emailWithoutValues[0]['EmailContent'][0]['mailc_content']);
    //print_r (htmlspecialchars($emailWithoutValues[0]['EmailTemplate']['mailt_content']));
	die("");
}

the prepareContent needs to be a function that verifies the text and replaces the words with $value

What is $emailWtithoutValues? An entity or an array?

Can you debug($emailWithoutValues) and show the result? It will make a difference in the solution

Never mind my previous reply. If I understand your problem correctly:

$this->LoadModel('Email');
$emailWithoutValues = $this->Email->getContentTemplate($id, $language);
$values = array("first_name"=>"Fredi","password"=>"test","discount_code"=>"DISC20","lala"=>"asdasd");

//Here is a solution if it is an entity
$email = $this->Email->patchEntity($emailWithoutValues, $values);

//Here are two solutions if it is an array

//Here's a step-at-a-time solution
$email = [];
foreach ($emailWithoutValues as $fieldName => $currentValue) {
    if (isset($values[$fieldName])) {
        $email[$fieldName] = $values[$fieldName];
    }
    else {
        $email[$fieldName] = $emailWithoutValues[$fieldName];
    }
}

//Here's the same thing using Cake's Collection and Hash classes
$email = (new Collection($emailWithoutValues))
    ->map(function($currentValue, $fieldName) use ($values) {
        return Hash::get($values, $fieldName) ?? $currentValue;
    })->toArray();

Read about patchEntity here

Read about collections here

Read about the Hash class here