php - Store a JavaScript array in the DB and get array back when needed -
i have multidimensional associative javascript array metabox data , need store array in wordpress database (in single column).
and want stored array in metabox call function , use values in array populate relevant fields.
this i've done far...
var data = [ { key: 'cardk', val: 13 }, { key: 'cardq', val: 12 }, { key: 'cardaj', val: 11 }, ]; var serializeddata= json.stringify( data );
this outputs string this
[[1,2],[3,4],[5,6]]
now can store in db in single column. how can array , use populate fields ?
save string json, this:
var data = [[1,2],[3,4],[5,6]]; var serializeddata = json.stringify(data);
before convert make sure replace data has changed in json make valid json before using json_decode()
.
use function:
str_replace();
to convert array on php side:
json_decode(data, true); // returns array json_decode(data); // returns object, not array.
when json string mysql, can turn array json.parse()
, this:
var returnedarr = json.parse(returneddata);
Comments
Post a Comment