SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    312
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    How to turn a json array created by PHP to javascript array

    I've created a jason array using a PHP json_encode() function. The result looks like this:
    Code:
    [{"track":0,"name":"Song1","file":"Song1.mp3"}
    ,{"track":1,"name":"Song2","file":"Song2.mp3"}
    ,{"track":2,"name":"Song3","file":"Song3.mp3"}]
    Then I turn it into a javascript string (not sure) with this method:
    Code:
    <script>
    	var mp3_tracks = '<?php echo json_encode($mp3_tracks); ?>';
    </script>
    I got the result as expected. However, when I extract javasacript values it looks ugly like this:
    Code:
    var track_1 = mp3_tracks[0]; // [
    var song_1 = mp3_tracks[0].file; // undefined
    When compare to this javascript array:
    Code:
    var mp3_tracks = [
    					{"track":0,"name":"Song1","file":"Song1.mp3"},
    					{"track":1,"name":"Song2","file":"Song2.mp3"},
    					{"track":2,"name":"Song3","file":"Song3.mp3"}
    				];
    var track_1 = mp3_tracks[0]; // Object {track: 0, name: "Song1", file: "Song1.mp3"} 
    var song_1 = mp3_tracks[0].file; // Song1.mp3
    How do I make the json block created created by PHP as javascript array.
    Thank you

  2. #2
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    simply
    PHP Code:
    <script>
        var mp3_tracks = <?php echo json_encode($mp3_tracks); ?>;
    </script>
    Bye.

  3. #3
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    312
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but the result is still the same. I'm pulling my hair off. (it was on chrome);

  4. #4
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    312
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Well, on a second attempt. It's kind of working. Still don't know why???

  5. #5
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,270
    Mentioned
    33 Post(s)
    Tagged
    5 Thread(s)
    Quote Originally Posted by ketting00 View Post
    Thanks, but the result is still the same. I'm pulling my hair off. (it was on chrome);
    Hi ketting00,

    Are you sure you've removed the quote marks around the php tag which outputs the json?

    Here's the code I'm using to test this in Chrome, and it's working properly:
    PHP Code:
    <?php
    $data 
    = array(
        array(
    "track" => 0"name" => "Song1""file" => "Song1.mp3"),
        array(
    "track" => 1"name" => "Song2""file" => "Song2.mp3"),
        array(
    "track" => 2"name" => "Song3""file" => "Song3.mp3")
    );
    ?>
    <!DOCTYPE html>
    <html>
      <head>
      <meta http-equiv="content-type" content="text/html; charset=utf-8">
      <title></title>
    </head>
      <body>
        <script>
            var tracks = <?php echo json_encode($data); ?>;
            console.log(tracks);
        </script>
      </body>
    </html>

  6. #6
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    312
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Here's my PHP code
    Code:
    $i = 0;
    while(($row = $result->fetch_object()) && ($i <= 3)) {
    	$mp3_tracks[] = array(
    		'track' => $i++,
    		'name' => str_replace(".".$mp3_title_extension,"",$mp3_title),
    		'file' => str_replace(".".$mp3_path_extension,"",$mp3_path)
    	);
    }
    It is working now. Thank you,

  7. #7
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,907
    Mentioned
    139 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by ketting00 View Post
    ... && ($i <= 3)
    Sounds like you need LIMIT 3 in your query
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  8. #8
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    312
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thanks. I wasn't thinking of that.


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
  •