1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Is there anyway in PHP to create variable variables?

Discussion in 'PHP' started by Imozeb, Apr 9, 2010.

  1. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #21
    I'm sorry but I am completely confused about PHP arrays. I read the array section and those two links you posted and I still can't get my code to work. Could you please give me an example of how to transfer a PHP array into a Javascript array. Thanks. :)
     
    Imozeb, Apr 11, 2010 IP
  2. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #22
    This code is bad...... by for example purposes only;

    <html><head>
    <script type="text/javascript">
    function parseJS(){
    var json=document.getElementById("json_textbox").value;
    var obj=eval('('+json+')');
    
    for(val in obj){
      alert(obj[val]); }
    }
    </script></head>
    <body>
    
    <?php
    $jsa = array(1,2,3,4,5);
    $json=json_encode($jsa);
    ?>
    <form>
    <input type="hidden" id="json_textbox" value='<?php print $json; ?>' />
    <input type='button' value="Do it!" onclick="parseJS();" />
    </form>
    <?print $json; ?>
    </body></html>
    Code (markup):
    Note: This only uses a 2d array, you will need to process it differently as passing an array of hashes will create a JS object (which, technically everything is in js anyway)....

    --> You could also process the JS inline within php tags in the JS function depending on the requirement of your JS.....

    --> As mentioned previously, You also have interchangeability by using serialize, which is the better option depending on the scope of your project and if the data is likely to be used/needs to be used elsewhere.....
     
    Last edited: Apr 12, 2010
    lukeg32, Apr 12, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #23
    Thanks lukeg32 for the example! It worked.
     
    Imozeb, Apr 12, 2010 IP