在我用PHP开发的这些年里,我总是听说使用eval()是邪恶的.
考虑以下代码,使用第二个(更优雅)选项是否有意义?如果没有,为什么?
// $type is the result of an SQL statement
// e.g. SHOW COLUMNS FROM a_table LIKE 'a_column';
// hence you can be pretty sure about the consistency
// of your string
$type = "enum('a','b','c')";
// possibility one
$type_1 = preg_replace('#^enum\s*\(\s*\'|\'\s*\)\s*$#', '', $type);
$result = preg_split('#\'\s*,\s*\'#', $type_1);
// possibility two
eval('$result = '.preg_replace('#^enum#','array', $type).';');
Run Code Online (Sandbox Code Playgroud) 我的第一个 JSON 是:
{
"categoryId":"Painting",
"subCategoryId":"Residential",
"alternatives": [1,2,3,4,5],
"criterias":["price","quantity","yom","company"],
"answers":[["1000","12343","4543","","4645646"],["12","23","34","","45"],["2014","","2000","1990","2005"],["xyz","","Abc","jkl","mno"]]
}
Run Code Online (Sandbox Code Playgroud)
这将来自一个 java URL,这里我使用的是 PHP,在 PHP 中我调用的是一个 java URL。
我的第二个 JSON 是:
{"criterias":"Location"}
Run Code Online (Sandbox Code Playgroud)
我正在使用 JQuery 生成它。如何将第二个 JSON 包含到第一个 JSON 标准中?