2013-04-21 103 views
1

我有一个数据库中的项目数组。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 
}); 

});

+0

尝试与各地<?PHP的双引号... – 2013-04-21 15:53:34

+1

那么你的问题是什么? – Musa 2013-04-21 15:56:26

+0

我认为你需要一个分号后,你的回声就像这样:''var echo ='?php echo json_encode($ this-> ingredientsHistory); ?>;' – 2013-04-21 16:06:16

回答

2

你的问题是,你必须从你的PHP数组中提取每个名称属性。

使用这样的

<?php 

$ingredientsArray = array(
    array('name' => "Lettuce"), 
    array('name' => "Butter"), 
    array('name' => "Chocolate"), 
    array('name' => "Cheese"), 
); // This is your original data structure 

$resultArray = array(); // Let's treat it to get a plain array of ingredient names 
foreach ($ingredientsArray as $ingredient) 
    $resultArray[] = $ingredient['name']; 

// And finally, output it 
echo json_encode($resultArray); 

如果你真的想要得到它在JS-只(虽然我不建议这样做,如果你在你的$ingredientsArray可变

$(function() { 
    // Your PHP-to-JSON stuff 
    var ingredientsArray = <?php echo json_encode($ingredientsArray); ?>; 

    var ingredientNames = []; // There are faster ways such as reduce, but it's the most browser-compatible way to do this. 
    for (int i = 0, l = ingredientsArray.length; i < l; ++i) 
     ingredientNames.push(ingredientsArray[i]['name']); 

    // Then assign it to your autocomplete plugin 
    $('#tags').autocomplete({ source: ingredientNames }); 
}); 
敏感数据

注意hasOwnProperty尚未使用,因为我们已经知道的数组中的数据格式。

+0

完美,我知道我正在处理一个多维数组,这就是为什么我发布代码如此深刻。我不确定是否有一种JavaScript的方式进入阵列!这虽然有用,但谢谢! – sark9012 2013-04-21 17:26:05

+0

你仍然可以用相同的方式在JS中迭代它,然后编辑我的帖子:) – 2013-04-21 17:30:52

0

您可以执行PHP迭代以将元素推入Array。

<script> 
var jsArray = new Array(); 
<?php 
    for ($this->ingredientsHistory as $value) 
?> 
jsArray.push(<?=$value?>); 
<?php 
    } 
?> 
</script> 
1
echo json_encode($this->ingredientsHistory); 

使用JSON的方式与客户进行沟通。简单,快速,广泛支持等。不可能更简单。而在客户端(JavaScript)中,您有JSON.stringifyJSON.parse来处理此问题。

原生支持是不可靠的,在传统模式下不能保证,但您可以使用OFFICIAL JSON实现来保证JSON的跨浏览器支持。如果您使用的是jQuery,如您所示,您不会遇到任何问题,只需使用jQuery方法即可。

此外,在PHP5,你可以使用

var ingredients = "<?= $this->ingredients; ?>"; 

如果内嵌处理失败,你总是可以简单地使用Ajax这一点。

$.ajax({ 
    url: "blabla", 
    data: "blabla", 
    type: "POST", 
    dataType: "json", 
    success: function(serverResponse) { 
     console.log(serverResponse); 
    } 
}); 

echo json_encode(whatever)上的Ajax目标网址。

+0

这不是OP已经在做什么? – millimoose 2013-04-21 15:56:08

+0

使它成为一个字符串只是让你有更多的工作要做。 – Musa 2013-04-21 15:57:21

+0

我已经这样做了,它不工作? – sark9012 2013-04-21 16:10:13