I have following multi dimensional array
$results = array(
'name' => 'signed_contract_message',
'required' => ':min',
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim')
),
'validators' => array(
array('name' => ':max'),
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => ':min',
'max' => ':max',
'values' => array(
'option_1' => ':min',
'option_2' => ':max'
)
)
)
)
);
I want to search and replace the occurrences of :min
and :max
which is stored in following array
$placeHolders = array(':min' => 2, ':max' => 100);
I created the following function to do the job
function searchAndReplacePlaceHolders($results, $placeHolders)
{
foreach ($results as $key => $result) {
if (is_string($result) && array_key_exists($result, $placeHolders)) {
$results[$key] = $placeHolders[$result];
}
if (is_array($result)) {
foreach ($result as $key1 => $result1) {
if (is_string($result1) && array_key_exists($result1, $placeHolders)) {
$results[$key][$key1] = $placeHolders[$result];
}
if (is_array($result1)) {
foreach ($result1 as $key2 => $result2) {
if (is_string($result2) && array_key_exists($result2, $placeHolders)) {
$results[$key][$key1][$key2] = $placeHolders[$result2];
}
if (is_array($result2)) {
foreach ($result2 as $key3 => $result3) {
if (is_string($result3) && array_key_exists($result3, $placeHolders)) {
$results[$key][$key1][$key2][$key3] = $placeHolders[$result3];
}
}
if (is_array($result3)) {
foreach ($result3 as $key4 => $result4) {
if (is_string($result4) && array_key_exists($result4, $placeHolders)) {
$results[$key][$key1][$key2][$key3][$key4] = $placeHolders[$result4];
}
}
}
}
}
}
}
}
}
return $results;
}
This works, but what i wanted to know is if we can improve this function or perhaps do it in better way ?
Thanks.