[英]How can i include loop for these nodes in javascript
I have these value in php 我在PHP中有这些价值
<?php $phpArray = array(
0 => "Mon",
1 => "Tue",
2 => "Wed",
3 => "Thu",
4 => "Fri",
5 => "Sat",
6 => "Sun",
);
?>
I get these values in a loop like this 我像这样循环获得这些值
<script type="text/javascript">
var jArray= <?php echo json_encode($phpArray ); ?>;
for(var i=0;i<6;i++){
// alert(jArray[i]);
}
</script>
But I have a situation like this where I need loop 但是我有这样的情况需要循环
{ data: { id: 'e', name: 'store', weight: 45, faveColor: '#EDA1ED', faveShape: 'ellipse' } },
Now i want that value on 'Store' and remains I want same ,I want to fetch all data through loop. 现在我想要在“ Store”上保留该值,并且仍然保持不变,我想通过循环获取所有数据。 How can i do this ?????
我怎样才能做到这一点 ?????
Thats relative easy: 多数民众赞成在相对容易:
<script type="text/javascript">
var jArray= [];
<? foreach($phpArray as $item): ?>
jArray.push("<?=$item?>");
<?endforeach;?>
</script>
try this 尝试这个
<?php
$phpArray = array(
"id" => "e",
"name" => "Store",
"weight" => 45,
"faveColor" => "#EDA1ED",
"faveShape" => "ellipse"
);
$arr_data['data'] = $phpArray
?>
<script type="text/javascript">
var str = '<?php echo stripslashes(json_encode($arr_data));?>';
alert(str); // output: {"data":{"id":"e","name":"Store","weight":45,"faveColor":"#EDA1ED","faveShape":"ellipse"}}
</script>
Please try this : 请尝试这个:
<script>
var jstring = {'data': { 'id': 'e', 'name': 'store', 'weight': 45, 'faveColor': '#EDA1ED', 'faveShape': 'ellipse'}};
for (key in jstring.data){
alert("key : " + key + " => " + "Value : " + jstring.data[key])
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.