我有一個數據庫中的項目數組。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