This article mainly shares with you how to obtain arrays in js and process php arrays. It is mainly shared with you in the form of code. I hope it can help you.
$music=array(); $music['tayler']=array(); $music['walker']=array(); $music['tayler'][]='ours'; $music['tayler'][]='style'; $music['tayler'][]='red'; $music['walker'][]='all fall down'; $music['walker'][]='faded'; $str=json_encode($music);
A php array $music and a $str converted into a json array have been written in the php background. Now the php background will pass these arrays to the current page.
<script type="text/javascript"> function changesinger(va,data){ var second = document.getElementById("second-data"); while(obj=second[0]){ obj.remove(); } <!-- var data=eval(<?php echo json_encode($music);?>); --> <!-- var data=<?php echo $str;?>; --> var data={$str}; for(var i=0;i<data[va].length;i++){ var opt=document.createElement("OPTION"); opt.text=data[va][i]; second.add(opt); } } </script>
Using php arrays in js needs to be converted into json format.
The first method: var data=eval();
Convert the php array into json encoding, and then use the eval function to convert it into a js array.
Second: var data=;
Here directly use the json encoded array in php,
Third Type: var data={$str};
The simple syntax of PHP is used directly here. {}Equivalent to echo ;?>
##Related recommendations:
Python processing PHP array text file example
The above is the detailed content of How to get array in js and process php array. For more information, please follow other related articles on the PHP Chinese website!