gpt4 book ai didi

javascript - 将js文件中的数组Fatch到php中,然后编码成json

转载 作者:行者123 更新时间:2023-11-28 07:10:50 24 4
gpt4 key购买 nike

我有一个js文件,其中包含一些数组形式的数据。 http://localhost/js/data.js 信息类似于

var subjectCodeArray=["CS/ Computer Science","AF/ Art Facalty",...]; 

我想将数组“subjectCodeArray”中的信息获取到我的php文件“subjectCode.php”中,然后将其编码为json数组。我不知道该怎么做,请帮我做一下。

提前致谢......

最佳答案

我首先得到了答案,从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);
}

最后编码为json形式 $output = json_encode(array('student_info'=>$json_response)); 回显$输出;

关于javascript - 将js文件中的数组Fatch到php中,然后编码成json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31317160/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com