如何在javascript中包含这些节点的循环

时间:2014-02-26 10:55:59

标签: javascript php

我在php中有这些价值

<?php $phpArray = array(
      0 => "Mon", 
      1 => "Tue", 
      2 => "Wed", 
      3 => "Thu",
      4 => "Fri", 
      5 => "Sat",
      6 => "Sun",
      );
?>

我在这样的循环中得到这些值

<script type="text/javascript">

var jArray= <?php echo json_encode($phpArray ); ?>;

for(var i=0;i<6;i++){
   // alert(jArray[i]);
}

 </script>

但我有这样的情况,我需要循环

{ data: { id: 'e', name: 'store', weight: 45, faveColor: '#EDA1ED', faveShape: 'ellipse'     } },

现在我希望“存储”上的值,并且我想要相同,我想通过循环获取所有数据。 我怎么能这样做?????

3 个答案:

答案 0 :(得分:0)

这很容易:

<script type="text/javascript">

var jArray= []; 

<? foreach($phpArray as $item): ?>
jArray.push("<?=$item?>");
<?endforeach;?>
</script>

答案 1 :(得分:0)

试试这个

<?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>

答案 2 :(得分:0)

请试试这个:

<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>