Jump to content

creating some arrays from ajax response text


sw45acp

Recommended Posts

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.

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?

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'];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.