我有一个类似于下面的数组结构:
$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}
语法时,它会有效,我收到以下错误:
解析错误:语法错误,意外'''
从我的字符串中取出{$}
解决了这个问题,所以这必须意味着双引号不会引发数组错误,但是{$}
抛出一个错误双引号。有什么想法吗?
修改
根据要求,以下是代码的设置方式:
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*/
}
答案 0 :(得分:1)
class Log_model extends CI_Model {
const SUPERVISOR_PROGRAM_UPDATED = 87;
private $system_data = array(
87 => array(
'description' => 'patient logged a new activity',
'message_type' => 3
)
)
private function changeMessage ($message_name) {
$system_data[87]['message'] = "{$message_name} logged a new activity.";
}
}
答案 1 :(得分:1)
有了MikeB和Zgr024的提示,这就是我所要达到的(不是最好的解决方案,但是有效的东西可以保持我脚本所需的灵活性和动态性):
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;
}
}