Jump to content

Merging two arrays(one multi dimensional) using a Regex & string replacement

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
kidder

kidder

    Newbie

  • Members
  • Pip
  • 1 posts
Hi Everyone,

A newbie here - but looking for some help to something that is killing my brain :).

What I want to do is to do a string replacement through one array(multi-dimensional) using values from another.

Here is what I have:
$regex = '/\{(.*?)}/i';
$options = array('username'=>'mjoe',
'maxage'=>'30',
'minage'=>'20'
);
$criteria = array('username' => '{username}',
'age'=>array('$lt' => '{maxage}','$gte' => '{minage}'),
);

What I want to end up with is the following:
$newarray = array('username' => 'mjoe',
'age'=>array('$lt' => '30','$gte' => '20'),
);

Basically replacing anything in the { } with the actual values from options.

Anyone have ideas on the best way to accomplish this? I have looked through all the php functions for str_replace but I am wondering if there is any easy way like using array_merge or something like that?

Any ideas would be greatly appreciated.

Thanks,
K

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
I'm pretty sure you are looking for either preg_replace or preg_replace_callback.