我使用php json_encode()函数()生成简单的json字符串。
array(3) {
[38105]=>
array(3) {
[0]=>
array(10) {
["value_id"]=>
string(5) "29428"
}
[1]=>
array(10) {
["value_id"]=>
string(5) "79332"
}
[2]=>
array(10) {
["value_id"]=>
string(5) "41199"
}
}
[38110]=>
array(3) {
[3]=>
array(10) {
["value_id"]=>
string(5) "29429"
}
[4]=>
array(10) {
["value_id"]=>
string(5) "79333"
}
[5]=>
array(10) {
["value_id"]=>
string(5) "41201"
}
}
[38115]=>
array(3) {
[6]=>
array(10) {
["value_id"]=>
string(5) "29430"
}
[7]=>
array(10) {
["value_id"]=>
string(5) "79334"
}
[8]=>
array(10) {
["value_id"]=>
string(5) "79334"
}
}
}
在模板php输出中:
<script type="text/javascript">
var ProductMediaGallery = <?php echo json_encode($block->getRelatedProductsGalleryImages()); ?>;
</script>
问题是这个函数将第一个集合解释为数组,并作为对象休息,所以我无法得到第一个集合。
function createZoomOverlay() {
if (typeof ProductMediaGallery !== 'undefined') {
$.each(ProductMediaGallery, function(key,value) {
console.log(value);
});
}
}
Chrome控制台输出
[Object, Object, Object]
Object {3: Object, 4: Object, 5: Object}
Object {6: Object, 7: Object, 8: Object}