【发布时间】:2015-06-15 06:16:01
【问题描述】:
我有以下 php 代码,它给出了一个名为“markers”的数组变量。
window.markers = [];
<?php if( have_rows('actin_center') ): ?>
<?php while( have_rows('actin_center') ): the_row(); ?>
window.markers.push( [ ['<?php the_sub_field('center_name'); ?>', '<?php the_sub_field('center_address'); ?>', <?php the_sub_field('latitude'); ?>, <?php the_sub_field('longitude'); ?>] ] );
<?php endwhile; ?>
<?php endif; ?>
到目前为止,这工作正常,但返回数组(处于警报状态)为:
Cool center 1,Rewitz Gofella, 1234 Lorem,50,50,Cool center 2,Lorem Ipsum, 1234 Quosque,60,60,Cool center 3,Veniat elaborat, 1234 Ipsum,70,70
然而,我需要的是以下形式,将(子字段的)数组保持原样,因为它们最初位于数组内部,而不是连接它们。如:
var markers = [
['First Center','First Address',50,50],
['Second Center','Second Address', -25.363882,131.044922],
['Third Center','Third Address', 10.363882,95],
['Fourth Center','Fourth Address', -50,-90],
['Fifth Center','Fifth Address', 30,5],
];
正如您在上面的代码中看到的那样,我尝试使用简单的双括号 [[ ]] 但这不起作用。 如何正确完成? 非常感谢您的帮助。
PS: 如果有人觉得有必要对我的问题投反对票,请告诉我原因,以便我可以学到一些东西。
【问题讨论】:
-
发布您的预期输出并输入
-
您会看到连接的数组,因为您正在警告数组,完成后调用加入数组的 toString() 函数。尝试使用
console.log(window.markers)并在控制台中查看您应该看到数组结构。 -
你不需要双括号。单括号是正确使用:
window.markers.push(['arrayItem1', 'arrayItem2']);。使用console.log(window.markers)检查您的输出。 -
或者,如果您仍然喜欢使用警报,请执行 alert(JSON.stringify(window.markers)); 以查看数组结构。
-
你们都非常正确!哇,今天学到东西了。事实上,我的代码一直都在工作,只是因为“错误”的警告消息而停止完成它!谢谢!!
标签: javascript php arrays