简体   繁体   中英

Ajax - PHP associative array into jquery associative array via ajax/json

I have the following hardcoded into jquery and I want to move the code over to pull the values from a database using ajax.

I get the data back and pass it through using json_encode but I need to keep the same format.

 codes['851'] = new Array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','110','120','205','206','207','208');

 codes['852'] = new Array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','110','120','205','206','207','208');

 codes['522'] = new Array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','120','205','206','207','208','209');

Here is the php array prior to json_encode.

$codes = array();
codes['851'] = array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','110','120','205','206','207','208');

codes['852'] = array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','110','120','205','206','207','208');

codes['522'] = array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','120','205','206','207','208','209');

I am trying to keep the same format as I do not want to rewrite all the other code in the script. Is it possible to match format?

if I understand right, you need format like this in your ajax responce.

codes['851'] = new Array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','110','120','205','206','207','208');
codes['852'] = new Array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','110','120','205','206','207','208');
codes['522'] = new Array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','120','205','206','207','208','209');

for PHP you need next:

$codes = array();
$codes['851'] = array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','110','120','205','206','207','208');
$codes['852'] = array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','110','120','205','206','207','208');
$codes['522'] = array('11','12','20','21','23','24','30','41','43','44','45','48','50','52','53','54','55','60','70','120','205','206','207','208','209');
echo 'codes='.json_encode($codes).';';

this is not similar visual, but equal in JS Object structure.

So the issue was in the ajax code, I had code outside of my ajax call that wasn't being called. After I moved the methods inside the success callback of the ajax call it all worked perfectly.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM