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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.