如何在PHP中创建这种类型的数组?

时间:2014-12-15 16:05:51

标签: javascript php arrays

我们可以在javascript中创建这种类型的数组 -

var locations = [
    ['Bondi Beach', -33.890542, 151.274856, 4],
    ['Coogee Beach', -33.923036, 151.259052, 5],
    ['Cronulla Beach', -34.028249, 151.157507, 3],
    ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
    ['Maroubra Beach', -33.950198, 151.259302, 1]
];

但是,我们如何在PHP中创建这种类型的数组? 什么是这种类型的数组?

谢谢,

3 个答案:

答案 0 :(得分:8)

如果您使用PHP >= 5.4putvande用于指出特定版本),请尝试此操作

$locations = [
    ['Bondi Beach', -33.890542, 151.274856, 4],
    ['Coogee Beach', -33.923036, 151.259052, 5],
    ['Cronulla Beach', -34.028249, 151.157507, 3],
    ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
    ['Maroubra Beach', -33.950198, 151.259302, 1]
];

如果您使用旧版本的PHP:

$locations = array(
    array('Bondi Beach', -33.890542, 151.274856, 4),
    array('Coogee Beach', -33.923036, 151.259052, 5),
    array('Cronulla Beach', -34.028249, 151.157507, 3),
    array('Manly Beach', -33.80010128657071, 151.28747820854187, 2),
    array('Maroubra Beach', -33.950198, 151.259302, 1)
);

您的其他问题

如评论所述,将来会问一个新的问题,因为这一方的格式是,一个问题,不同的Soltution。

  

Works ....我想把这个数组传递给javascript函数参数,   我怎么能够实现这个目标呢?

我已经回答了一个类似问题here,基本上你是echo Javascript的方式相同,你会使用HTML

echo "var locations = [
    ['$sampleVar1', $sampleVar2, $sampleVar3, $sampleVar4],
    ['Coogee Beach', -33.923036, 151.259052, 5],
    ['Cronulla Beach', -34.028249, 151.157507, 3],
    ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
    ['Maroubra Beach', -33.950198, 151.259302, 1]
];";

答案 1 :(得分:0)

通常将数据作为内部数组(多维数组)。

$arr = array(array('Bondi Beach', -33.890542, 151.274856, 4),
array('Manly Beach', -33.80010128657071, 151.28747820854187, 2),
array('Maroubra Beach', -33.950198, 151.259302, 1));

答案 2 :(得分:0)

这样的事情应该有效..

$locations=array(
    0=>array('Bondi Beach', -33.890542, 151.274856, 4),
    1=>array('Coogee Beach', -33.923036, 151.259052, 5),
    2=>array('Cronulla Beach', -34.028249, 151.157507, 3),
    3=>array('Manly Beach', -33.80010128657071, 151.28747820854187, 2),
    4=>array('Maroubra Beach', -33.950198, 151.259302, 1)
);