问题描述
我有一个类似于下面的数组结构:
I have an array structure similar to below:
$system_data = array(
87 => array(
'message' => "{$message_name} logged a new activity.",
'description' => 'patient logged a new activity',
'message_type' => 3
)
)
调用函数时,它会找到引用的消息字符串(来自ID-87),并将相关的 $ message_name
变量值注入字符串。如果我返回一个直接将变量直接插入的静态双引号字符串,则可以使用,但是当我将其转换为数组时(更改字符串的双引号并添加 {$ variable_name}
语法错误,出现以下错误:
When a function is called, it finds the referred message string (from the ID - 87) and injects the related $message_name
variable value into the string. It works if I return a static double quoted string that directly injects the variable, but when I transposed this to my array (changing for double quotes on the string and adding the {$variable_name}
syntax to it, I get the following error:
从字符串中删除 {$}
即可解决此问题,因此必须意味着引号不会引发数组错误,而是 {$}
会引起双引号错误。有什么想法吗?
Taking the {$}
out of my string solves the problem, so that must mean that the double quotes aren't throwing an error with the array, but that it's the {$}
throwing an error with the double quotes. Any ideas?
编辑:
根据要求,代码设置如下:
As requested, here's how the code is set:
class Log_model extends CI_Model {
const SUPERVISOR_PROGRAM_UPDATED = 87;
private $system_data = array(
87 => array(
'message' => "{$message_name} logged a new activity.",
'description' => 'patient logged a new activity',
'message_type' => 3
)
)
/*functions here*/
}
推荐答案
有了MikeB和Zgr024的提示,这就是我要解决的问题(不是最好的解决方案,而是可以工作并保持灵活性和动态性的东西)需要我的脚本):
With MikeB and Zgr024's hints, this is what I've come to (not the best solution, but something that works and keeps the flexibility and dynamicness i need for my script):
class Log_model extends CI_Model {
const SUPERVISOR_PROGRAM_UPDATED = 87;
private static $system_data = array(
87 => array(
'message' => "{$message_name} logged a new activity.",
'description' => 'patient logged a new activity',
'message_type' => 3
)
)
/*data is a set of values (key->value) to be injected into the string (included and retrieved depending on the log_type_id needed when calling this function)*/
public function generate_message($log_type_id = FALSE, $data = array()){
$message = self::$system_data [$log_type_id]['message'];
foreach($data as $key=>$value){
$message = str_replace("{%".$key."%}", $value, $message, $i);
}
return $message;
}
}
这篇关于解析错误:数组中出现意外的双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!