sw45acp Posted August 24, 2010 Share Posted August 24, 2010 Hi, I have an ajax response text string that looks something like this: "part2=true;part7=false;part9=false;..." I would like to turn this string into an array, that looks like this: part2 = true part7 = false part9 = false I know to use the explode function, but that will only break it into an array either by using the "=" or ";" as a delimiter.... Thank you for any help. Quote Link to comment Share on other sites More sharing options...
Adam Posted August 24, 2010 Share Posted August 24, 2010 An AJAX response..? I'm not sure I follow. The 'AJAX response' would be handled at the JS end. I have a possible better solution for you, but just need to know exactly what you mean first. Quote Link to comment Share on other sites More sharing options...
sw45acp Posted August 24, 2010 Author Share Posted August 24, 2010 Its an ajax response text, from an ajax request using prototype. The text comes back from the request from the server like "part1=true;part4=false". I need to be able to break it into an array for sorting. Quote Link to comment Share on other sites More sharing options...
Adam Posted August 24, 2010 Share Posted August 24, 2010 So this is with JavaScript? You've posted in a PHP forum talking about explode .. a PHP function. Quote Link to comment Share on other sites More sharing options...
sw45acp Posted August 24, 2010 Author Share Posted August 24, 2010 Ahh, youre right i posted this in the wrong forum. I hate it when I do that. yes this needs to be done with javascript since its being handled in the ajax request. Sorry im kind of a noob. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted August 24, 2010 Share Posted August 24, 2010 Aside from that fact, why not encode it with JSON then decode the response text, saves sending back a string to break apart. Quote Link to comment Share on other sites More sharing options...
sw45acp Posted August 24, 2010 Author Share Posted August 24, 2010 Aside from that fact, why not encode it with JSON then decode the response text, saves sending back a string to break apart. So you mean encode javascript in the server side where the response text comes from? Is http://www.prototypejs.org/learn/json this what you mean? Quote Link to comment Share on other sites More sharing options...
Adam Posted August 24, 2010 Share Posted August 24, 2010 No worries, I was just a little confused. Okay, well my advise would be to use a better data structure. Have you heard of JSON before? The PHP side is easy, all you need to do is run your array through json_encode and output to the browser. When the JavaScript receives it as the response text, JS can already read it; you just access it like a normal array. Quick example: <?php $array = array( 'part2' => true, 'part7' => false, 'part9' => false ); echo json_encode($array); ?> That will return: {"part2":true,"part7":false,"part9":false} Then in your JavaScript you can just access it like a normal array: var part2 = ajax.responseText['part2']; Quote Link to comment Share on other sites More sharing options...
sw45acp Posted August 24, 2010 Author Share Posted August 24, 2010 Ok that looks like it will do the trick. Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.