我有一个php multidimesional数组:
<script src="http://code.jquery.com/jquery-1.11.0.js"></script>
<?php
$brand = array (
"nike" => array (
array( all deal 1 info in here),
array( all deal 2 info in here),
array( all deal 3 info in here)),
"puma" = array(
array( all deal 1 info in here),
array( all deal 2 info in here),
array( all deal 3 info in here)),
"addidas" = array(
array( 'logo'=>'images/addidas.png', 'description'=>'some addidas text'),
array( all deal 2 info in here),
array( all deal 3 info in here)),
);
?>
<script type="text/javascript">
var brand = <?php echo json_encode($brand) ?>;
for (var n in brand){
for (var i in brand[n]){
// for jQuery("body").append jQuery is necessary, but of course you can work otherwise with this data, too
jQuery("body").append('<a href="' + brand[n][i].logo + '">' + brand[n][i].description + '</a>');
}
}
我想要做的是将其转换为javascript数组,循环并打印出每个品牌作为超链接。因此,如果有人点击了addidas,这会在容器div中显示内容。到目前为止,我已经尝试了这些建议,但它的表现并不像我想的那样。
我想将品牌名称显示为div中的超链接,如果您单击说Addidas链接,它会将Addidas内容仅提取到内容div:
<div id="brand-nav">
<a links here>
</div>
<div id="deals-content">
on click of a link, brand specific content here
</div>
感谢大家的贡献,所以任何额外的帮助都非常感谢。
Volterony
答案 0 :(得分:0)
您可以将其转换为JSON数组
var brand= "<?php echo json_encode($brand) ?>"
答案 1 :(得分:0)
通过json_encode将您的PHP数组转换为javascript数组,然后迭代它,例如将它附加到身体上:
var brand = <?php echo json_encode($brand) ?>;
for (var n in brand){
for (var i in brand[n]){
// for jQuery("body").append jQuery is necessary, but of course you can work otherwise with this data, too
jQuery("body").append('<a href="' + brand[n][i].logo + '">' + brand[n][i].description + '</a>');
}
}
答案 2 :(得分:0)
在使用上述方法之一将其转换为javascript之前,我会更改数组的结构:
$brands = array(
array(
'name' => 'nike',
'deals' => array(
array('all deal 1 info in here'),
array('all deal 2 info in here'),
array('all deal 3 info in here'),
),
),
array(
'name' => 'puma',
'deals' => array(
array('all deal 1 info in here'),
array('all deal 2 info in here'),
array('all deal 3 info in here'),
),
),
array(
'name' => 'adidas',
'deals' => array(
array('logo' => 'images/addidas.png', 'description' => 'some addidas text'),
array('all deal 2 info in here'),
array('all deal 3 info in here'),
),
),
);
原因是关联数组是地图,但看起来你正在存储一个品牌列表,而不是硬编码你拥有的品牌数量。
我知道PHP可以让你轻松地遍历关联数组(事实上javascript与对象一样)但我相信这是不好的做法......
编辑:添加了PHP解决方案(仅限显示)
<?php foreach ($brands as $brand): ?>
<a href="<?php echo $brand['url'] ?>" title="<?php echo $brand['description'] ?>"><img src="<?php echo $brand['logo'] ?>" alt="<?php echo $brand['description'] ?>" /></a>
<?php endforeach ?>
编辑:我读了所有这些javascript的东西并认为你需要它...如果你不需要转换为javascript,请忽略下面
然后使用上面的数组,你可以把它放到你的javascript中,如其他答案所述:
var brands = <?php json_encode($brands) ?>;
品牌将在哪里:
[
{
"name": "nike",
"deals": [
{
// All deal 1 info here
},
{
// All deal 2 info here
},
{
// All deal 3 info here
},
]
},
{
"name": "puma",
"deals": [
{
// All deal 1 info here
},
{
// All deal 2 info here
},
{
// All deal 3 info here
},
]
},
{
"name": "adidas",
"deals": [
{
"logo": "images/adidas.png",
"description": "some adidas text"
},
{
// All deal 2 info here
},
{
// All deal 3 info here
},
]
},
]