String to Array

  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 453

Post 3+ Months Ago

Whenever I post here it’s never a simple issue, Today I need a make a string into an array. Seems simple enough, but here’s the fun part no use of .split() as a solution by itself will suffice (I’ll explain below) nor the use of eval(). Though eval works great for what I need to do I need to eliminate eval() from this project. A function that works just like eval would be great.

Why I can’t use .split()

I’m using AJAX to return a string in the form of a JavaScript array, normally I’d use split, but do to the complexity of what needs to happen just using .split() would make things a nightmare.

JAVASCRIPT Code: [ Select ]
 
// PHP will return a string that most the time looks like this
new Array("Errors", "Info", "Info", new Array("info",  "info"));
 
// Also potentially return
new Array("Errors", "Info", "Info", new Array("info",  "info", new Array("info",  "info", new Array("info",  "info"))));
 
  1.  
  2. // PHP will return a string that most the time looks like this
  3. new Array("Errors", "Info", "Info", new Array("info",  "info"));
  4.  
  5. // Also potentially return
  6. new Array("Errors", "Info", "Info", new Array("info",  "info", new Array("info",  "info", new Array("info",  "info"))));
  7.  


So as you can see there are multidimensional arrays that I need to be converted from a string to an Array. As I stated before eval was great for handling this however it is a suspect in my IE crashing issue in which I’m still dealing with.
I am still actively searching for solutions and will post my findings and solutions if I happened to come across any.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 453

Post 3+ Months Ago

Ok I'm giving up, Kinda, and decided to have PHP spit out a serialized array and then have the javascript equivalant to the unserialize function i found at http://phpjs.org/ to make it back into an array.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9087
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Hi Scott,

That was one thing I was going to recommend, was either serializing it or encoding it in a JSON format if you use the jQuery library like I do. When I do AJAX, I usually have any PHP scripts return the data in a JSON format by doing this on the PHP end:

PHP Code: [ Select ]
   header("Cache-Control: no-cache");  
   echo json_encode(array("string1", "string2", "string3"));
   exit;
  1.    header("Cache-Control: no-cache");  
  2.    echo json_encode(array("string1", "string2", "string3"));
  3.    exit;


Then with jQuery you would simply decode it like so:

JAVASCRIPT Code: [ Select ]
$.ajax({
   type:"post"
   url: "test.php",
   async:true,
   dataType:"json",
   success: function(result){
      //result holds data sent from the php script already in an array that can be accessed result[0], result[1], etc
   }
});
  1. $.ajax({
  2.    type:"post"
  3.    url: "test.php",
  4.    async:true,
  5.    dataType:"json",
  6.    success: function(result){
  7.       //result holds data sent from the php script already in an array that can be accessed result[0], result[1], etc
  8.    }
  9. });


I love jQuery and how simple it makes things. Even though it takes a bit to load up the library, overall I write so much less JavaScript that I save on the amount of resources the client has to load, as well as the time I spend writing code.
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 453

Post 3+ Months Ago

I haven't jumped onto the jQuery boat just yet. mainly because the site I'm currently rebuilding was made with a jQuery based frame work and was an absolute nightmare to debug. I understand jQuery and when I do have free time may invest some tie in working with it. When I get around to making the design of my own website (instead of other peoples) I was actually going to try to get into jQuery with my own site.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9087
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Yeah that make sense, I remember that to be the case for me when I was working on a client site with prototype (another Javascript library). It was difficult at that time to debug things because I did not write the code was not sure on how it all worked.

I do recommend you try jQuery with your own site, once you are familiar with it you will find its a powerful, time saving library. The fact it is well supported over numerous browsers allows you to not have to figure out why things work in FF but not IE either. Most of the trivial tasks are already done for you.

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: hFJyCxL1 and 136 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.