Results 1 to 8 of 8

Thread: PHP array into javascript array

  1. #1
    Join Date
    Feb 2009
    Posts
    156
    Thanks
    0
    Thanked 4 Times in 3 Posts

    Default PHP array into javascript array

    hi,
    i have an array into PHP and i want to use it into javascript..
    canany1 help my by guiding or giving me the coding ?
    cz i want to processed that data on client,.....


    plzreplysoon

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    To cover all possible sorts of arrays could get complex, but this looks good for basic stuff:

    http://forums.devshed.com/showpost.p...05&postcount=4
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Use PHP to echo the data in the array as text in a javascript format.

    For example:
    echo 'var = ['.implode(', '$array).'];';

    That's a simple array... it can certainly get more complex.

    A foreach loop in php can work like this:
    foreach ($array as $key=>$value) {
    //do whatever you want
    }

    $key is the key, and $value is the value.

    If it needs to be multi-layered, then you'll need a function and if statements to echo strings and start a new layer of the function for arrays.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Dec 2009
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    or you can use:

    echo 'var = '.json_encode($array).';';

    It will work also with multi-dimension arrays.

  5. The Following User Says Thank You to simplecode For This Useful Post:

    djr33 (12-30-2009)

  6. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by simplecode View Post
    or you can use:

    echo 'var = '.json_encode($array).';';

    It will work also with multi-dimension arrays.
    That looks to be very useful if it does as one might imagine. However, care should be exercised:

    PHP 5 >= 5.2.0, PECL json >= 1.2.0
    from:

    http://us2.php.net/manual/en/function.json-encode.php
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #6
    Join Date
    Feb 2009
    Posts
    156
    Thanks
    0
    Thanked 4 Times in 3 Posts

    Default

    Quote Originally Posted by simplecode View Post
    or you can use:

    echo 'var = '.json_encode($array).';';

    It will work also with multi-dimension arrays.

    its giving square breckets in output...
    c this code

    <?php
    $arr = array ('a','b','c','d','e');

    echo "var a =" . json_encode($arr)."";
    ?>



    out put is:

    var a =["a","b","c","d","e"]



    but i want out put like this :
    var a ={"a","b","c","d","e"}
    so it can be used as array


    plz give me some readymade coding for this

  8. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    This (what you say you are getting):

    Code:
    var a =["a","b","c","d","e"]
    is a javascript array.

    This:

    Code:
    var a ={"a","b","c","d","e"}
    is a javascript error, an invalid object initializer.

    Try:

    PHP Code:
    <script type="text/javascript">
    <?php
    $arr 
    = array ('a','b','c','d','e');

    echo 
    "var a =" json_encode($arr)."";
    ?>
    ;alert (a[1]);
    </script>
    or, more properly:

    PHP Code:
    <script type="text/javascript">
    <?php
    $arr 
    = array ('a','b','c','d','e');
    echo 
    'var a = ' json_encode($arr) . ";\n";
    ?>
    alert(a[1]);
    </script>
    Both work.

    Note: Requires:

    PHP 5 >= 5.2.0, PECL json >= 1.2.0
    Last edited by jscheuer1; 01-01-2010 at 03:41 PM. Reason: add demos
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #8
    Join Date
    Dec 2009
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by gurmeet View Post
    but i want out put like this :
    var a ={"a","b","c","d","e"}
    so it can be used as array
    This give you javascript syntax error.

    Try test this sample
    Code:
    <html>
        <script>
            <?php
            $arr1 = array ('a','b','c','d','e');
            echo "var a =" . json_encode($arr1).";";
            ?>
            alert("a: "+a[1]);
         </script>
    </html>
    {} brackets can be used only with associative array.

    NOTE:
    If you don't have PHP 5 >= 5.2.0, PECL json >= 1.2.0, or want portable code, use this

    Code:
    if (!function_exists('json_encode')){
        function json_encode($a){
            $str = '{';
            $dlm = '';
            if (is_array($a)){
                foreach ($a as $key => $item){
                    if (is_array($item)){
                        $str.= $dlm.'"'.$key.'":'.my_json_encode($item).'';
                    }else{
                        $str.= $dlm.'"'.$key.'":"'.$item.'"';
                    }
                    $dlm = ',';
                }
            }
            $str .= '}';
            return $str;
        }
    }
    Last edited by simplecode; 01-01-2010 at 04:42 PM.

Bookmarks

Posting Permissions

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