1000+
Reviews!
1000+ Reviews!
SEARCH SITE
Favorites
Cart
SEARCH
1000+ Reviews!
Click To Call Us Now!
SEARCH
Favorites
Cart
SAVE THE DATES
WEDDING INVITATIONS
THANK YOU CARDS
EXTRAS
MAKE DEPOSIT
BLOG
MY CART
MY ACCOUNT
CONTACT US
Parent ID is 768 and parent title is Info Dump Back to Info Dump Index
SEE THE CONSOLE FOR RESULTS OF THE TESTS BELOW
Test 1: Pass a PHP indexed array directly to a JS variable... it says that variable is an array type (with no data)
Test 2: Pass a PHP indexed array wrapped in quotes to a JS variable... it says that variable is the string "Array"
Test 3: Pass a json_encoded PHP indexed array to a JS variable, NOT wrapped in quotes... you get a JS array
Test 4: Pass a json_encoded PHP indexed array wrapped in quotes to a JS variable... you get a string in JSON format... ["Value 1", "Value 2", etc.]
Test 5: Pass a PHP indexed array (not json_encoded) and parse it on the JS side NOT wrapped in quotes before assigning it to a JS variable... you get an ERROR
Test 6: Pass a PHP indexed array (not json_encoded) and parse it on the JS side AFTER wrapping it in quotes and assigning it to a JS variable... you get an ERROR (it tries to parse 'A')
Test 7: Pass a json_encoded PHP indexed array and parse it on the JS side NOT wrapped in quotes before assigning it to a JS variable... you get an ERROR
Test 8: Pass a json_encoded PHP indexed array and parse it on the JS side AFTER wrapping it in quotes and assigning it to a JS variable... you get a JS array (same as test 3, but more complicated)
SWITCH GEARS FROM RECEIVING A SIMPLE INDEXED ARRAY TO RECEIVING A JSON STRING FORMATTED AS AN INDEXED ARRAY OF ASSOCIATIVE ARRAYS
(I'm talking formatted with [ ] to represent the outer indexed array and { } for each inner associative array... [{A:1, B:2, C:3},{D:4, E:5, F:6}, etc.])
Test 9: Don't parse it, you simply get a long string in JSON format
Test 10: Parse the incoming string and you get an indexed array of associative arrays (well, objects since JS doesn't have associative arrays)
Bottom Line:
(1) If it's an array, encode it before you pass from PHP to JS
(2) If it doesn't encode to [{},{},{}] format then format it correctly before you pass it from PHP to JS
(3) If it's an indexed array you don't need to parse it in JS
(4) If it's an associative array or something fancier (like an indexed array of associative arrays), you DO need to parse it in JS
(5) Don't try to parse something that's not a string, wrap it in quotes (unless it's an indexed array)
(6) If you wrap an incoming indexed array in quotes it will rended as a string in JSON format. You can parse it, OR just remember bottom line #1