How to get array in js and process php array

小云云
Release: 2023-03-21 18:16:01
Original
1985 people have browsed it

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);
Copy after login

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template