Home >Backend Development >PHP Tutorial >How to get array in js and process php array

How to get array in js and process php array

小云云
小云云Original
2018-03-20 10:18:232017browse

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(993003aaac23667cffe472760c42e099);

Convert the php array into json encoding, and then use the eval function to convert it into a js array.

Second: var data=aef70eaf44f5de0f9eaa20def19c695d;

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 ca31a91dc15ebc2648450ed222ddb7bd

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn