www.webdeveloper.com
Results 1 to 3 of 3

Thread: PHP array to JavaScript array?

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Location
    Gujarat, India.
    Posts
    35

    Question PHP array to JavaScript array?

    Hi,

    Q.1 :
    i want to know that how i can set php (array) variable to javascript variable?
    i have a php array like,
    $array = array(1,2,3);
    i know i can set variable by
    var x = <?php echo $array[1]; ?>
    but problem starts from here, that "1" (in above line, $array[ 1 ] wouldn't be always one it may be, 0,1,2.. anything. So how can i do it?

    And Q.2 : Can i copy whole php array to javascript array?

    Thanks in advance!

  2. #2
    Join Date
    May 2006
    Location
    North of the South Pole
    Posts
    590
    You can use JSON to get the PHP array to a JavaScript array.

    Code:
    var x = JSON.parse('<?php echo json_encode($array); ?>');
    "I have no special talent. I am only passionately curious." Albert Einstein

  3. #3
    Join Date
    Oct 2012
    Location
    Gujarat, India.
    Posts
    35
    Yeah, thanks for answer! However i found it someday ago.. and i am using it. But it is like
    Code:
    var x = <?php echo json_encode($array) ?>
    and after it i have x[0], x[1] according to $array[0], $array[1].
    Do i need to use json.parse()?
    And is there any other answer for Q.1?

    EDIT: P.S. loved your signature shane.carr
    Last edited by deepode; 03-27-2013 at 06:20 AM. Reason: Comment about signature!

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