PHP使用字符串替换从多维数组中的键值替换关键字

时间:2014-08-13 14:54:53

标签: php arrays string laravel dynamic-arrays

我有像这样的多维数组

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数组?

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

4 个答案:

答案 0 :(得分:0)

来自php.netmixed 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 );
}

答案 1 :(得分:0)

以下是一些评论代码。只要键的数量与输入数组中的元素数匹配,这应该适用于您。这也假设您的占位符与您的密钥名称匹配。

//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.
//)

在线演示here

答案 2 :(得分:0)

最好的办法是让第一个阵列采用这种格式:

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
}

答案 3 :(得分:0)

假设您的标题数组为$titles,如果模板中的占位符与标题相同(例如{From}而不是From),那么您可以通过< / p>

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
}