How do I write array with variable in array in JavaScript

Discussion in 'JavaScript' started by ketting00, Jun 20, 2016.

  1. #1
    Hi guys,

    In PHP, I have an array which look like this:
    
    $arr = ['x' => [$a,$b]];
    
    Code (markup):
    How do I write this in JavaScript?

    Thanks,
     
    Solved! View solution.
    ketting00, Jun 20, 2016 IP
  2. hdewantara

    hdewantara Well-Known Member

    Messages:
    538
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #2
    Such operations like below seem to be okay in javascript:
    var arr = [];
    arr['x'] = ['hello', 'world'];
    Code (JavaScript):
    ?
    Hendra
     
    hdewantara, Jun 20, 2016 IP
    Sugavanas likes this.
  3. ketting00

    ketting00 Well-Known Member

    Messages:
    782
    Likes Received:
    28
    Best Answers:
    3
    Trophy Points:
    128
    #3
    Hi Hendra,

    Thanks. That works.
     
    ketting00, Jun 20, 2016 IP
  4. ketting00

    ketting00 Well-Known Member

    Messages:
    782
    Likes Received:
    28
    Best Answers:
    3
    Trophy Points:
    128
    #4
    Wooh!

    I use it like this:
    
    var xhr = new XMLHttpRequest();
    xhr.open('GET', 'index.php?x[0]=' + var_a + '&x[1]=' + var_b, true);
    xhr.send();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            var msg = JSON.parse(xhr.responseText);
        }
    }
    
    Code (markup):
    To solve ajax request with link like this:
    
    <a href="index.php?<?php echo http_build_query(['x' => [$a,$b]]); ?>">Get Data</a>
    
    Code (markup):
     
    ketting00, Jun 20, 2016 IP
  5. hdewantara

    hdewantara Well-Known Member

    Messages:
    538
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #5
    Hey that's new to me; the http_build_query(). Thx :).

    It seems like you need to process the $arr['x'] in client's browser javascript first before sending the ajax, right? Else, a direct use of the http_build_query() might simplify the steps a bit, I guess:
    xhr.open('GET', 'index.php?<?php echo http_build_query(["x" => [$a,$b]]); ?>', true);
    PHP:
    Hmm, doesn't look nice though :eek:
     
    Last edited: Jun 20, 2016
    hdewantara, Jun 20, 2016 IP
  6. ketting00

    ketting00 Well-Known Member

    Messages:
    782
    Likes Received:
    28
    Best Answers:
    3
    Trophy Points:
    128
    #6
    It looks nice. With this, you can encrypt data both in PHP and JavaScript, making it harder a bit for hacker to attack.

    Ajax can't do something like this if you use the js file:
    
    xhr.open('GET', 'index.php?<?php echo http_build_query(["x" => [$a,$b]]); ?>', true);
    
    Code (markup):
     
    Last edited: Jun 20, 2016
    ketting00, Jun 20, 2016 IP
  7. hdewantara

    hdewantara Well-Known Member

    Messages:
    538
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #7
    Ah, right. I forgot that js should be external whenever possible.
     
    hdewantara, Jun 22, 2016 IP
  8. #8
    No no no, Javascript is not like php, you couldn't use associative array, you should use object.
    
    var arr = {};
    arr['x'] = ['hello', 'world'];
    Code (JavaScript):
     
    fehtrar, Jul 12, 2016 IP
    ketting00 and hdewantara like this.
  9. hdewantara

    hdewantara Well-Known Member

    Messages:
    538
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #9
    My mistake, sorry.
    Yes it should be an object.
     
    hdewantara, Jul 12, 2016 IP
  10. ketting00

    ketting00 Well-Known Member

    Messages:
    782
    Likes Received:
    28
    Best Answers:
    3
    Trophy Points:
    128
    #10
    Thanks for pointing that out. I'll go back and have it corrected. Do you have any good links which I can read about? My website is JavaScript heavy, sort of.
     
    ketting00, Jul 13, 2016 IP
  11. hdewantara

    hdewantara Well-Known Member

    Messages:
    538
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #11
    hdewantara, Jul 13, 2016 IP
    ketting00 likes this.