我有一组来自数据库的项目。
`$this->ingredientsHistory` is the variable that contains the array.
我想把它转换成一个 javascript var 来保存它们,然后我可以在自动完成 jquery UI 函数中使用它们。
我试过了var ingredients = <?php echo json_encode($this->ingredientsHistory); ?>
这是一个print_r($this->ingredientsHistory);
输出示例...
数组 ( [0] => 数组 ( [name] => 橙子 ) [1] => 数组 ( [name] => 鸡 ) )
任何帮助,将不胜感激。
编辑 - 更多信息:
$(function() {
var ingredients = <?php echo json_encode($this->ingredientsHistory); ?>;
console.log(ingredients);
//this is the default tags that jquery gives me - i need to turn ingredients into something similar.
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
$( "#tags" ).autocomplete({
source: ingredients
});
});