How to return an array from a Javascript function ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #16
    If you need the keys too, add them to the string too, e.g.
    [code=javascript]for (var i in obj) {
    str += i + ":" + obj + "|";
    }[/code]

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #17
      [code=javascript]var aArray = new Array;
      aArray['key'] = new Array;
      aArray['value'] = new Array;
      var key;
      for(key in yourArray) {//yourArray is your original array
      aArray['value'].push(yourArray[key]);
      aArray['key'].push(key);
      }[/code]

      Hope you could utilize aArray

      Comment

      • pankajit09
        Contributor
        • Dec 2006
        • 296

        #18
        Originally posted by acoder
        If you need the keys too, add them to the string too, e.g.
        [code=javascript]for (var i in obj) {
        str += i + ":" + obj + "|";
        }[/code]

        I don't want the values in a string.

        I just want to return the associative array.

        Comment

        • pankajit09
          Contributor
          • Dec 2006
          • 296

          #19
          Originally posted by hsriat
          [code=javascript]var aArray = new Array;
          aArray['key'] = new Array;
          aArray['value'] = new Array;
          var key;
          for(key in yourArray) {//yourArray is your original array
          aArray['value'].push(yourArray[key]);
          aArray['key'].push(key);
          }[/code]

          Hope you could utilize aArray

          You are creating two arrays.

          I want a single associative array.

          Comment

          • hsriat
            Recognized Expert Top Contributor
            • Jan 2008
            • 1653

            #20
            Originally posted by pankajit09
            I don't want the values in a string.

            I just want to return the associative array.
            Tell me one thing first...
            What is an associative array?
            Code:
            arr[0] = 'a'
            arr[1] = 'b'
            arr[2] = 'c'
            arr[3] = 'd'
            arr[4] = 'e'
            If this is an array, what results do you want?

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #21
              Originally posted by pankajit09
              I don't want the values in a string.

              I just want to return the associative array.
              We already did that earlier with the return statement. I thought you wanted to pass this to a PHP script via Ajax.

              Comment

              • pankajit09
                Contributor
                • Dec 2006
                • 296

                #22
                Originally posted by hsriat
                Tell me one thing first...
                What is an associative array?
                Code:
                arr[0] = 'a'
                arr[1] = 'b'
                arr[2] = 'c'
                arr[3] = 'd'
                arr[4] = 'e'
                If this is an array, what results do you want?

                Actually I want to send an associative array to a PHP file via Ajax without converting it to a string.

                arr["asasa"] = 'a'
                arr["wew"] = 'b'
                arr["rtr"] = 'c'

                Comment

                • hsriat
                  Recognized Expert Top Contributor
                  • Jan 2008
                  • 1653

                  #23
                  Okey... if that is the case, you have to send it as a string only.
                  In PHP, you can use explode to convert your string back to an array.

                  Use what acoder suggested in post#16.
                  Then when your variables is received by your PHP script.
                  Use:[php]$your_final_arr ay("key"=>array (), "value"=>array( ));
                  $your_array = explode(",", $_POST['your_array_str ing']); //or GET... which ever you using
                  foreach ($your_array as $each_value)
                  {
                  array_push($you r_final_array['key'], strtok($each_va lue, ":"));
                  array_push($you r_final_array['value'],strtok(":"));
                  }[/php]

                  Comment

                  Working...