-1

hi i have my data in a string like this

Array
(
    [0] => Array
        (
            [pollId] => 2
            [poll] => new
            [cId] => 7
            [communityId] => 7
            [ansId] => Array
                (
                    [0] => 6
                    [1] => 7
                )

            [answer] => Array
                (
                    [0] => new1
                    [1] => new2
                )

            [vote] => Array
                (
                    [0] => 37
                    [1] => 36
                )

            [count] => 2
        )

)

i want to convert it to array.

11
  • I don't know but I am not getting you. You have array in to string and want to convert string to array, right Commented Jan 22, 2011 at 13:51
  • Which of those have you tried? stackoverflow.com/search?q=convert+php+array+to+javascript Commented Jan 22, 2011 at 13:52
  • possible duplicate of Best method for converting a PHP array to javascript Commented Jan 22, 2011 at 13:53
  • 1
    and what part does jquery have in this question? It's not clear for me Commented Jan 22, 2011 at 13:53
  • @Ass3mbler The part where jquery is confused for javascript I think. Commented Jan 22, 2011 at 13:55

1 Answer 1

2

If you really have a string containing the text as quoted, you're going to have to either find or write a parser for it.

It's not a million miles off JSON, and so you might get a leg up on your implementation (if you have to write one) from the two non-eval implementations of JSON parsers found on Crockford's github page. There's json_parse.js, which is a recursive-descent parser; and json_parse_state.js which is a state machine. In both cases, they parse JSON, not your example format, but my point is that as your example format isn't a million miles away from JSON, you can probably use them as a starting point.

Update: (I'd added a comment about this, but it's better as part of the answer.) I see from the comment stream on your question that you don't have to use this format, but instead can use JSON instead. In that case, you're in good shape — jQuery has JSON parsing built in, in its parseJSON function. That will accept a string containing JSON-encoded data and return a JavaScript object graph (which may or may not be an array, depending on what the JSON defines). If you retrieve the JSON via Ajax, jQuery will even parse it for you automatically and give you the resulting object graph as an argument to your success function, see $.ajax and $.getJSON. Here's a live example I did for another question just earlier today. That loads the JSON found at the URL you'll see in the code, has jQuery automatically parse it, and accesses one of the properties of the resulting JavaScript object graph.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks T.J. Crowder. actually its not a JSON i simply return it from my controller in codeigniter framework.but i can convert it into JSON. if you have soluation to convert JSON into jquery array then please tell me. thanks in advance
@enthusiastic: I've updated the answer, since you've now said you can use JSON instead.
J.Crowder thanks. but its not working for me. i use success: function(msg){alert(msg.poll);});. its showing undefined
@enthusiastic: Take a look at your JSON. If it's an equivalent to what you posted in your question (but in JSON format), you'd want msg[0].poll, because the top level of your data structure is an array, whereas poll is a property on the first entry in the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.