使用string_replace从多维数组中的键值替换关键字


PHP Replace keyword from key-value in multidimension array using string_replace

我有一个像这样的多维数组

Array
(
    [0] => Array
        (
            [0] => Foo
            [1] => Bar
            [2] => I like foobar
            [3] => 09/09/2014
        )
    [1] => Array
        (
            [0] => Foo2
            [1] => Bar2
            [2] => Very much
            [3] => 10/09/2014
        )
)

键数组看起来像这样

Array
    (
        [0] => From
        [1] => To
        [2] => Text
        [3] => Schedule Date
    )

消息是一个字符串变量

$message = "Hi {To} message is from {From} come get {Text}.

我的问题是我如何在同一时间为所有数组值替换{$key}之间的关键字,以产生包含替换关键字的消息的新$messages数组?

这必须动态完成,而不是硬编码,因为每次都会使用不同的值

下面是一些注释代码。只要键的数量与输入数组中的元素数量相匹配,这应该就可以工作。这也假设您的占位符匹配您的键名。

//input data    
$inputs = array();
$inputs[0][] = 'Foo';
$inputs[0][] = 'Bar';
$inputs[0][] = 'I like foobar';
$inputs[0][] = '09/09/2014';
$inputs[1][] = 'Foo2';
$inputs[1][] = 'Bar2';
$inputs[1][] = 'Very much';
$inputs[1][] = '10/09/2014';
//keys for the input data
$keys = array('From', 'To', 'Text', 'Schedule Date');
//message template
$message = "Hi {To} message is from {From} come get {Text}.";
$messages = array();
//Loop through the input data
foreach($inputs as $input) {
   $input = array_combine($keys, $input); //give input array our custom keys
   $userMessage = $message; //create a copy of the message template
   //Loop through the values replacing `{$key}` with $value
   foreach($input as $key=>$value) { 
       $userMessage = str_replace('{'.$key.'}', $value, $userMessage);
   }
   //Do something with the message
   $messages[] = $userMessage;
}
print_r($messages);
//Outputs:
//Array
//(
//    [0] => Hi Bar message is from Foo come get I like foobar.
//    [1] => Hi Bar2 message is from Foo2 come get Very much.
//)

在线演示

From php.net: mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

假设你有:

Array1 (original): $arr1
Array2 (replace): $arr2

你可以这样做:

foreach($arr2 as $key => $value) 
{
    $newMessage = str_replace ($arr1[$key], $value, $message, 1 );
}

最好使用这种格式的第一个数组:

Array
(
    [0] => Array
    (
        ['From'] => 'Foo',
        ['To'] => 'Bar',
        ['Text'] => 'Message text',
        ['Schedule_Date'] => '01/01/01',
    ),
    ...
)

那么你可以在循环中这样做:

$message = "Hi {To} message is from {From} come get {Text}.";
foreach ($array as $toSend) {
    $messageReplaced = $message;
    foreach ($toSend as $key => $value) {
        $messageReplaced = str_replace('{' . $key . '}', $value, $messageReplaced);
    }
    $this->mail->send($messageReplaced, $toSend['Schedule_Date']); // just an example
}

假设您的标题数组是$titles,如果模板中的占位符与标题相同(例如{From}而不是From),那么您可以轻松地使用

进行替换
foreach($data as $row) {
    $result = str_replace($titles, $row, $template);
    // now do something with $result
}

由于这非常方便,而且占位符几乎与标题相同,因此很容易先将标题转换为占位符,然后再执行上述操作:

$titles = ['From', 'To', 'Text'];
$placeholders = array_map(function($t) { return '{'.$t.'}'; }, $titles);
foreach($data as $row) {
    $result = str_replace($placeholders, $row, $template);
    // now do something with $result
}