I'm trying to store an array in the value of an element, and then use ajax to print the array, but when I tried to print the array, using print_r(), instead of printing the array, it printed the word array. What am I doing wrong?
<?php
$array[0] = 0;
$array[1] = 1;
$array[2] = 2;
echo "<input id = 'array' type = 'hidden' value = '$array'>";
//more codes
?>
In JavaScript:
var array = $("#"+array).val();
//more codes
data: { array: array },
On Second Page
array = $_POST['array'];
print_r(array);
Result
array
Expected Result
0
1
2
var array = $("#"+array).val();==>var array = $("#array").val();json_encodewhile outputting the value on theinputfield and on next page before using the value usejson_decode. You can also useserializeandunserializeto achieve the same effect