简体   繁体   中英

How can i include loop for these nodes in javascript

I have these value in 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. 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM