[英]Fatch array from a js file into php and then encode into a json
I have a js file which contains some data in the array form. 我有一个js文件,其中包含一些数组形式的数据。 http://localhost/js/data.js The information is something like
http://localhost/js/data.js信息类似于
var subjectCodeArray=["CS/ Computer Science","AF/ Art Facalty",...];
and I want to get the information from the array "subjectCodeArray" into my php file "subjectCode.php" and then want to encode it into json array. 我想从数组“ subjectCodeArray”中获取信息到我的php文件“ subjectCode.php”中,然后将其编码为json数组。 I don't know how to do it pleas help me to do it.
我不知道该怎么做,请帮我做。
thanks in advance..... 提前致谢.....
I got the answer first of all fatch the data from js file $data = file_get_contents("./data.js"); 我首先从js文件$ data = file_get_contents(“ ./ data.js”);中获取数据。
Create a substring which contain the data from starting "[" to the ending "]"
$substrdata = substr($data, strpos($data,'[')+1, strpos($data,']') - strpos($data,'['));
then replase "\" with space
$substrdata = str_replace("\"", "", $substrdata);
then replace the singal cotes
$substrdata = str_replace("'", "", $substrdata);
and the explode it in the bases of comma and store it in array
$arr=explode(",",$substrdata);
$json_response = array();
for($i=0;$i<count($arr);$i++)
{
$row_array['student'] = preg_replace("/\//",'',$arr[$i]);
array_push($json_response,$row_array);
}
and finally encode in json form $output = json_encode(array('student_info'=>$json_response)); 最后以json形式编码$ output = json_encode(array('student_info'=> $ json_response)); echo $output;
echo $ output;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.