我有一个数据库中的项目数组。Array to javascript
`$this->ingredientsHistory` is the variable that contains the array.
我想这个转换成JavaScript变种,将保存所有,然后我可以在自动完成的jQuery UI功能使用它们。
我试图var ingredients = <?php echo json_encode($this->ingredientsHistory); ?>
这是一个print_r($this->ingredientsHistory);
输出的一个例子...
阵列([0] =>阵列([名称] =>桔子)[1] = >阵列([名称] =>鸡))
任何帮助,将不胜感激。
编辑 - 的更多信息:
$(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
});
});
尝试与各地<?PHP的双引号... – 2013-04-21 15:53:34
那么你的问题是什么? – Musa 2013-04-21 15:56:26
我认为你需要一个分号后,你的回声就像这样:''var echo ='?php echo json_encode($ this-> ingredientsHistory); ?>;' – 2013-04-21 16:06:16