Passing a PHP associative array to JavaScript

Discussion in 'Javascript' started by bruce, Mar 7, 2011.

  1. bruce

    bruce Guest

    How do I parse a PHP associative array with JavaScript?

    I want to returning from the server some database elements using
    AJAX.

    1. Will AJAX allow my to return to a PHP associative array?

    2. If so, how do I retrieve the data,ie parse the array into
    elements? Would it be any better to return an indexed array?

    Thanks for the help...

    Bruce
    bruce, Mar 7, 2011
    #1
    1. Advertising

  2. bruce

    RobG Guest

    On Mar 8, 7:48 am, bruce <> wrote:
    > How do I parse a PHP associative array with JavaScript?
    >
    > I want to returning from the server some database elements using
    > AJAX.
    >
    > 1. Will AJAX allow my to return to a PHP associative array?
    >
    > 2. If so, how do I retrieve the data,ie parse the array into
    > elements?  Would it be any better to return an indexed array?


    <URL: http://www.json.org/ >

    --
    Rob
    RobG, Mar 7, 2011
    #2
    1. Advertising

  3. bruce

    Ross McKay Guest

    On Mon, 7 Mar 2011 13:48:42 -0800 (PST), bruce wrote:

    >How do I parse a PHP associative array with JavaScript?


    You are best served by turning the array into something that JavaScript
    can handle, e.g. XML or JSON.

    http://en.wikipedia.org/wiki/XML
    http://en.wikipedia.org/wiki/JSON

    >I want to returning from the server some database elements using
    >AJAX.
    >
    >1. Will AJAX allow my to return to a PHP associative array?
    >
    >2. If so, how do I retrieve the data,ie parse the array into
    >elements? Would it be any better to return an indexed array?


    Unless your PHP is providing data to other clients, I'd return the data
    as JSON to make it easy to deal with in JavaScript. PHP has a function
    to convert data into JSON:

    http://au.php.net/manual/en/function.json-encode.php

    And there's plenty of options for parsing the JSON in JavaScript:

    http://json.org/
    https://developer.mozilla.org/en/JSON
    --
    Ross McKay, Toronto, NSW Australia
    "Let the laddie play wi the knife - he'll learn"
    - The Wee Book of Calvin
    Ross McKay, Mar 7, 2011
    #3
  4. bruce

    bruce Guest

    On Mar 7, 6:09 pm, Ross McKay <>
    wrote:
    > On Mon, 7 Mar 2011 13:48:42 -0800 (PST), bruce wrote:
    > >How do I parse a PHP associative array with JavaScript?

    >
    > You are best served by turning the array into something that JavaScript
    > can handle, e.g. XML or JSON.
    >
    > http://en.wikipedia.org/wiki/XMLhttp://en.wikipedia.org/wiki/JSON
    >
    > >I want to returning from the server some database elements using
    > >AJAX.

    >
    > >1. Will AJAX allow my to return to a PHP associative array?

    >
    > >2. If so, how do I retrieve the data,ie parse the array into
    > >elements?  Would it be any better to return an indexed array?

    >
    > Unless your PHP is providing data to other clients, I'd return the data
    > as JSON to make it easy to deal with in JavaScript. PHP has a function
    > to convert data into JSON:
    >
    > http://au.php.net/manual/en/function.json-encode.php
    >
    > And there's plenty of options for parsing the JSON in JavaScript:
    >
    > http://json.org/https://developer.mozilla.org/en/JSON
    > --
    > Ross McKay, Toronto, NSW Australia
    > "Let the laddie play wi the knife - he'll learn"
    > - The Wee Book of Calvin


    Thanks...
    json does the job..

    Bruce
    bruce, Mar 8, 2011
    #4
  5. bruce

    bruce Guest

    On Mar 7, 7:16 pm, bruce <> wrote:
    > On Mar 7, 6:09 pm, Ross McKay <>
    > wrote:
    >
    >
    >
    > > On Mon, 7 Mar 2011 13:48:42 -0800 (PST), bruce wrote:
    > > >How do I parse a PHP associative array with JavaScript?

    >
    > > You are best served by turning the array into something that JavaScript
    > > can handle, e.g. XML or JSON.

    >
    > >http://en.wikipedia.org/wiki/XMLhttp://en.wikipedia.org/wiki/JSON

    >
    > > >I want to returning from the server some database elements using
    > > >AJAX.

    >
    > > >1. Will AJAX allow my to return to a PHP associative array?

    >
    > > >2. If so, how do I retrieve the data,ie parse the array into
    > > >elements?  Would it be any better to return an indexed array?

    >
    > > Unless your PHP is providing data to other clients, I'd return the data
    > > as JSON to make it easy to deal with in JavaScript. PHP has a function
    > > to convert data into JSON:

    >
    > >http://au.php.net/manual/en/function.json-encode.php

    >
    > > And there's plenty of options for parsing the JSON in JavaScript:

    >
    > >http://json.org/https://developer.mozilla.org/en/JSON
    > > --
    > > Ross McKay, Toronto, NSW Australia
    > > "Let the laddie play wi the knife - he'll learn"
    > > - The Wee Book of Calvin

    >
    > Thanks...
    > json does the job..
    >
    > Bruce


    Sorry, but now I'm having trouble retrieving the data..

    This alert shows the complete array, correctly.
    alert("detail Result: " + strResult);

    Both of these alerts display: Undefined
    alert(strResult['newspaper']);
    alert(strResult.newspaper);

    What am I doing wrong??
    bruce, Mar 8, 2011
    #5
  6. bruce

    bruce Guest

    On Mar 7, 7:55 pm, bruce <> wrote:
    > On Mar 7, 7:16 pm, bruce <> wrote:
    >
    >
    >
    > > On Mar 7, 6:09 pm, Ross McKay <>
    > > wrote:

    >
    > > > On Mon, 7 Mar 2011 13:48:42 -0800 (PST), bruce wrote:
    > > > >How do I parse a PHP associative array with JavaScript?

    >
    > > > You are best served by turning the array into something that JavaScript
    > > > can handle, e.g. XML or JSON.

    >
    > > >http://en.wikipedia.org/wiki/XMLhttp://en.wikipedia.org/wiki/JSON

    >
    > > > >I want to returning from the server some database elements using
    > > > >AJAX.

    >
    > > > >1. Will AJAX allow my to return to a PHP associative array?

    >
    > > > >2. If so, how do I retrieve the data,ie parse the array into
    > > > >elements?  Would it be any better to return an indexed array?

    >
    > > > Unless your PHP is providing data to other clients, I'd return the data
    > > > as JSON to make it easy to deal with in JavaScript. PHP has a function
    > > > to convert data into JSON:

    >
    > > >http://au.php.net/manual/en/function.json-encode.php

    >
    > > > And there's plenty of options for parsing the JSON in JavaScript:

    >
    > > >http://json.org/https://developer.mozilla.org/en/JSON
    > > > --
    > > > Ross McKay, Toronto, NSW Australia
    > > > "Let the laddie play wi the knife - he'll learn"
    > > > - The Wee Book of Calvin

    >
    > > Thanks...
    > > json does the job..

    >
    > > Bruce

    >
    > Sorry, but now I'm having trouble retrieving the data..
    >
    > This alert shows the complete array, correctly.
    > alert("detail Result: " + strResult);
    >
    > Both of these alerts display: Undefined
    > alert(strResult['newspaper']);
    > alert(strResult.newspaper);
    >
    > What am I doing wrong??


    I forgot to mention that strResult is returned from AJAX..
    bruce, Mar 8, 2011
    #6
  7. bruce

    bruce Guest

    On Mar 7, 8:10 pm, bruce <> wrote:
    > On Mar 7, 7:55 pm, bruce <> wrote:
    >
    >
    >
    > > On Mar 7, 7:16 pm, bruce <> wrote:

    >
    > > > On Mar 7, 6:09 pm, Ross McKay <>
    > > > wrote:

    >
    > > > > On Mon, 7 Mar 2011 13:48:42 -0800 (PST), bruce wrote:
    > > > > >How do I parse a PHP associative array with JavaScript?

    >
    > > > > You are best served by turning the array into something that JavaScript
    > > > > can handle, e.g. XML or JSON.

    >
    > > > >http://en.wikipedia.org/wiki/XMLhttp://en.wikipedia.org/wiki/JSON

    >
    > > > > >I want to returning from the server some database elements using
    > > > > >AJAX.

    >
    > > > > >1. Will AJAX allow my to return to a PHP associative array?

    >
    > > > > >2. If so, how do I retrieve the data,ie parse the array into
    > > > > >elements?  Would it be any better to return an indexed array?

    >
    > > > > Unless your PHP is providing data to other clients, I'd return the data
    > > > > as JSON to make it easy to deal with in JavaScript. PHP has a function
    > > > > to convert data into JSON:

    >
    > > > >http://au.php.net/manual/en/function.json-encode.php

    >
    > > > > And there's plenty of options for parsing the JSON in JavaScript:

    >
    > > > >http://json.org/https://developer.mozilla.org/en/JSON
    > > > > --
    > > > > Ross McKay, Toronto, NSW Australia
    > > > > "Let the laddie play wi the knife - he'll learn"
    > > > > - The Wee Book of Calvin

    >
    > > > Thanks...
    > > > json does the job..

    >
    > > > Bruce

    >
    > > Sorry, but now I'm having trouble retrieving the data..

    >
    > > This alert shows the complete array, correctly.
    > > alert("detail Result: " + strResult);

    >
    > > Both of these alerts display: Undefined
    > > alert(strResult['newspaper']);
    > > alert(strResult.newspaper);

    >
    > > What am I doing wrong??

    >
    > I forgot to mention that strResult is returned from AJAX..


    OKAY.. I figured it out.
    var temp = "values = " + strResult;
    eval(temp);
    alert(values['newspaper']);
    alert(values.newspaper);

    This makes it work..

    Thanks for all your help...

    Bruce
    bruce, Mar 8, 2011
    #7
  8. bruce

    Ross McKay Guest

    On Mon, 7 Mar 2011 17:16:54 -0800 (PST), bruce wrote:

    >OKAY.. I figured it out.
    > var temp = "values = " + strResult;
    > eval(temp);
    > alert(values['newspaper']);
    > alert(values.newspaper);


    Or you can do this:

    var parseJSON = function(data) {
    return window.JSON ? JSON.parse(data) : eval("(" + data + ")");
    };

    var values = parseJSON(strResult);

    The eval() requires an expression, so wrapping your JSON packet in
    parentheses allows eval() to ... um ... evaluate it :)

    It's a little simplistic, but functional, and it will avail itself of a
    built-in JSON parser on browsers that provide one.

    You might like to load Crockford's json2.js to provide a JSON.parse
    across browsers instead:

    https://github.com/douglascrockford/JSON-js
    --
    Ross McKay, Toronto, NSW Australia
    "Let the laddie play wi the knife - he'll learn"
    - The Wee Book of Calvin
    Ross McKay, Mar 8, 2011
    #8
  9. bruce

    bruce Guest

    On Mar 7, 8:41 pm, Ross McKay <>
    wrote:
    > On Mon, 7 Mar 2011 17:16:54 -0800 (PST), bruce wrote:
    > >OKAY.. I figured it out.
    > >    var temp = "values = " + strResult;
    > >    eval(temp);
    > >    alert(values['newspaper']);
    > >    alert(values.newspaper);

    >
    > Or you can do this:
    >
    >  var parseJSON = function(data) {
    >      return window.JSON ? JSON.parse(data) : eval("(" + data + ")");
    >  };
    >
    >  var values = parseJSON(strResult);
    >
    > The eval() requires an expression, so wrapping your JSON packet in
    > parentheses allows eval() to ... um ... evaluate it :)
    >
    > It's a little simplistic, but functional, and it will avail itself of a
    > built-in JSON parser on browsers that provide one.
    >
    > You might like to load Crockford's json2.js to provide a JSON.parse
    > across browsers instead:
    >
    > https://github.com/douglascrockford/JSON-js
    > --
    > Ross McKay, Toronto, NSW Australia
    > "Let the laddie play wi the knife - he'll learn"
    > - The Wee Book of Calvin


    Thanks... I'm using your suggestion....

    Appreciate the comeback...

    Bruce
    bruce, Mar 8, 2011
    #9
  10. On 07/03/11 21:48, bruce wrote:
    > How do I parse a PHP associative array with JavaScript?
    >
    > I want to returning from the server some database elements using
    > AJAX.
    >
    > 1. Will AJAX allow my to return to a PHP associative array?
    >
    > 2. If so, how do I retrieve the data,ie parse the array into
    > elements? Would it be any better to return an indexed array?


    I normally pass these as a json string containing an object where
    object.attribute = value

    for example, in my php I can create the json text:

    ----8<----8<----8<----8<----
    <?php
    header("Content-Type: text; charset=utf-8");
    $str = "{ 'title' : 'Mr' , 'firstnames' : 'john henry' , 'lastname' :
    'smith' , 'height' : 180 , 'weight' : 110 }";
    echo $str;
    ?>
    ----8<----8<----8<----8<----

    Note that there is no blank line at the end of the file, and no "\n" is
    used, just a single one line string.

    Next, in javascript I can use an XmlHttpRequest(xhr) object to request
    the php file from the server, which will get a text response back, and
    in the xhr processing, eval the text response:

    var thePerson = JSON.parse('(' + responseText + ')');

    then, in javascript:

    thePerson.title = 'Mr'
    thePerson.firstnames = 'john henry'
    thePerson.lastname = 'smith'
    thePerson.height = 180 (cms I guess)
    thePerson.weight = 110 (kgs I guess)

    Note that JSON.parse may not exist in older browsers, so you can either
    supply it, or use the dreaded eval instead. It also leaves out a lot of
    the javascript surrounding the use of ajax and the xhr object.

    Rgds

    Denis McMahon
    Denis McMahon, Mar 8, 2011
    #10
  11. bruce

    Ross McKay Guest

    On Tue, 8 Mar 2011 11:01:52 -0800 (PST), "J.R." wrote:

    >It's not safe to test for native JSON support by testing for 'window.JSON'
    >existence, in order to avoid errors in older browsers. I prefer to use:
    >
    >var rsp; // defined inside a function
    >if (typeof JSON !== "undefined") {
    > try {
    > // IE8, FF3.5, Safari4, Chrome4, Opera10.5
    > rsp = JSON.parse(data);
    > } catch (ex) { // alert('Error:\n\n' + ex.description); }
    >} else {
    > // suggested by http://tools.ietf.org/html/rfc4627
    > rsp = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
    > data.replace(/"(\\.|[^"\\])*"/g, ''))) &&
    > eval('(' + data + ')');
    >}


    Fair point, but the OP is still probably better off using Crockford's
    json2.js which deals with these issues, and more. As I stated:

    >> It's a little simplistic, but functional, and it will avail itself of a
    >> built-in JSON parser on browsers that provide one.
    >>
    >> You might like to load Crockford's json2.js to provide a JSON.parse
    >> across browsers instead:
    >>
    >> https://github.com/douglascrockford/JSON-js

    --
    Ross McKay, Toronto, NSW Australia
    "Tuesday is Soylent Green day"
    Ross McKay, Mar 9, 2011
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. desktop
    Replies:
    5
    Views:
    384
    James Kanze
    Jun 26, 2007
  2. Rajive Narain
    Replies:
    0
    Views:
    1,735
    Rajive Narain
    Sep 18, 2009
  3. Yvon Thoraval
    Replies:
    5
    Views:
    205
    Jason Creighton
    Sep 17, 2003
  4. Remi Bastide

    PHP-Like associative array initialization ?

    Remi Bastide, Jan 6, 2005, in forum: Javascript
    Replies:
    2
    Views:
    81
    Remi Bastide
    Jan 6, 2005
  5. VK
    Replies:
    36
    Views:
    642
    Martin Honnen
    Aug 3, 2005
Loading...

Share This Page