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.
I think using array_walk_recursive
function would be the best solution in such case:
array_walk_recursive($results, function(&$v, $k, $ph){
if (is_string($v) && array_key_exists($v, $ph)) {
$v = $ph[$v];
}
}, $placeHolders);
// the final output is shown below:
array(4) {
["name"]=>
string(23) "signed_contract_message"
["required"]=>
int(2)
["filters"]=>
array(2) {
[0]=>
array(1) {
["name"]=>
string(9) "StripTags"
}
[1]=>
array(1) {
["name"]=>
string(10) "StringTrim"
}
}
["validators"]=>
array(2) {
[0]=>
array(1) {
["name"]=>
int(100)
}
[1]=>
array(2) {
["name"]=>
string(12) "StringLength"
["options"]=>
array(4) {
["encoding"]=>
string(5) "UTF-8"
["min"]=>
int(2)
["max"]=>
int(100)
["values"]=>
array(2) {
["option_1"]=>
int(2)
["option_2"]=>
int(100)
}
}
}
}
}
I would suggest using a recursive functoin which will check if the next element is an array and if so would go one level deeper and if the next element is the wanted one change it with the needed value something like this:
function recurseArray(&$array, $val)
{
if(is_array($array))
{
foreach($array as $key=>&$arrayElement)
{
if(is_array($arrayElement))
{
recurseArray($arrayElement, $val);
}
else
{
if($arrayElement == $val)
{
//do your magic
}
}
}
}
}