I'm creating PHP scraper to get value from Javascript array, how to Convert Javascript array to PHP Array
<?php
// example scraped array
$str = 'var str_array = ["aa", "bb"]';
$str = str_replace("var ", '$', $str);
eval($str);
print_r($str_array);
Post your javascript array as a JSON string via ajax and process it server-side.
Javascript
var str_array = ["aa", "bb"];
var request = $.ajax({
url: "test.php",
method: "POST",
data: { myData : JSON.stringify(str_array) },
dataType: "html"
});
request.done(function( msg ) {
// ajax response
});
request.fail(function( jqXHR, textStatus ) {
alert( "Request failed: " + textStatus );
});
PHP (test.php)
$json = $_POST['myData'];
$myDataArray = json_decode($json,true);
print_r($myDataArray);
Eval() should end with ;
<?php
$str = 'var str_array = ["aa", "bb"]';
$str = str_replace("var ", '$', $str);
eval($str.";");
print_r($str_array);
Output:
Array ( [0] => aa [1] => bb )
But I don't know why you opt for this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.