www.webdeveloper.com
Results 1 to 9 of 9

Thread: Javascript array from php array

  1. #1
    Join Date
    Oct 2011
    Posts
    29

    Javascript array from php array

    I've been using the following code to create a one dimensional javascript array from a php array, and it has been working great...

    var js_array = new Array();
    js_array = [' . ($php_array ? '"' . implode('", "', $php_array) . '"' : '') . '], i;

    how can I append the above code to work for a multidimensional array.

    Thanks for any suggestions

  2. #2
    Join Date
    May 2005
    Location
    Dirty Jersey
    Posts
    1,402
    i haven't worked with php in a while; provide an example of a sample multidimensional php array AFTER it's been added to a webpage. using that as a template, it can be formed into JS.

  3. #3
    Join Date
    Oct 2011
    Posts
    29
    this works perfect for a 1-D array...

    <?php
    $php_array = array('a',0,1);
    echo '

    function print_array()
    {
    var js_array = new Array();
    js_array = [' . ($php_array ? '"' . implode('", "', $php_array) . '"' : '') . '], i;

    for(var i = 0; i <= 2; i++)
    { alert(js_array[i]); }
    }

    ';
    ?>

  4. #4
    Join Date
    Oct 2011
    Posts
    29
    so my questions is how to structure the implode statement for a 2-D array...

    <?php
    $php_array = array(array('a',0,1),array('b',0,1));
    echo '

    function print_array()
    {
    var js_array = new Array();
    js_array = [' . ($php_array ? '"' . implode('", "', $php_array) . '"' : '') . '], i;
    for(var i = 0; i <= 2; i++)
    { alert(js_array[i]); }
    }

    ';
    ?>

  5. #5
    Join Date
    Feb 2012
    Posts
    218
    Use json functions from PHP.
    PHP Code:
    $array = array(
            array(
    123'a'),
            array(
    456'b')
        ); 
    Code:
    var js_array = <?php echo json_encode($array)?>;
    http://uk3.php.net/json_encode

    This worls with n-dimesional arrays. If you set keys, the array will be transformed into an object.
    Last edited by hyperionXS; 03-04-2012 at 02:09 PM.

  6. #6
    Join Date
    Oct 2011
    Posts
    29
    Thanks Hyperion that worked great.

  7. #7
    Join Date
    May 2005
    Location
    Dirty Jersey
    Posts
    1,402
    learned something knew. i didn't know that PHP knew how to return JSON.

    i know at this point, it will be a dead issue to you mielke, but what i meant for you to do is to show me the OUTPUT of one of your multidimensional arrays, not the PHP code to create/access the array.

  8. #8
    Join Date
    Feb 2012
    Posts
    218
    This is the output of the above function
    Code:
    var js_array = [[1,2,3,"a"],[4,5,6,"b"]];

  9. #9
    Join Date
    May 2005
    Location
    Dirty Jersey
    Posts
    1,402
    exactly what i would have been looking for in mielke's second post had i needed to proceed without the json_encode knowledge i just gained.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles