Loading ...
Sorry, an error occurred while loading the content.
 

Re: connection manager - passing an associative array

Expand Messages
  • foggjacob
    I made a pretty good solution (it can even handle multi-dimension as long as you make sure to use different delimiters) In PHP you create your array and add
    Message 1 of 2 , May 31, 2007
      I made a pretty good solution (it can even handle multi-dimension as
      long as you make sure to use different delimiters)

      In PHP you create your array and add some items:
      $stateArr = array();
      $stateArr['modules_on'] = $moduleStr;
      $stateArr['modules_selected'] = sess("modules_selected");
      $stateArr['objects_on'] = $objectStr;
      $stateArr['objects_selected'] = sess("objects_selected');


      Then use the following function and return the result:
      echo implode_with_key($stateArr);
      function implode_with_key($assoc, $inglue = '=>', $outglue = '|'){
      $return = '';
      foreach ($assoc as $key => $value){
      $return .= $outglue . $key . $inglue . $value;
      }
      return substr($return,strlen($outglue));
      }


      Then, back in JavaScript you'll receive the following string:
      var str =
      "modules_on=>123|modules_selected=>3|objects_on=>1234|objects_selected=>2"


      So you use the following JavaScript function to place it back into an
      array:
      var arr = explode_Arr(str);
      function explode_Arr(str,inglue,outglue){
      inglue = (inglue)?inglue:'|';
      outglue = (outglue)?outglue:'=>';
      var startArr = str.split(inglue);
      var retArr = new Array();
      for(var i in startArr){
      var tempArr = startArr[i].split(outglue);
      var key = tempArr[0];
      retArr[key] = tempArr[1];
      }
      return retArr;
      }

      Now you have your associative array for full use in JavaScript!

      *NOTE* Keep in mind that in JavaScript there is no true associative
      array, it is actually just an object that works on the basis that
      "object['foo'] = 'bar';" is the same as "object.foo = 'bar';". With
      that said, you cannot use the built-in JS array function on the pseudo
      associative arrays (like myArr.length)... but if you need to iterate
      through the values just use the "for(var i in myArr){}" syntax.



      --- In ydn-javascript@yahoogroups.com, "foggjacob" <foggjacobn@...> wrote:
      >
      > I am using PHP's SESSION variables to maintain and process/update the
      > state of my application.
      >
      > I am using JavaScript to manipulate the visual state of objects within
      > my UI.
      >
      > I am using YUI's Connection Manager to make an Ajax call to a PHP page
      > to pass a string containing an '|' delimited result. I then use JS to
      > turn that back into an array and break it apart...
      >
      > my problem is that I currently have to refer to each of the array
      > items as myArr[0], myArr[1], etc... which works as long as the sending
      > structure is exactly like the receiving. For future ease and
      > compatibility I would like to be able to pass an associative array so
      > that I can simply refer to myArr['objects'], myArr['actions'] etc.
      >
      > It would also be useful to be able to pass multi-deminsional arrays,
      > though it's not necessary.
      >
      > Any ideas on how this can be accomplished?
      >
      > I though about doing some fancy double delimiting such as: function
      > implode_with_key($assoc, $inglue = '=>', $outglue = '|'){
      > $return = '';
      > foreach ($assoc as $tk => $tv){
      > $return .= $outglue . $tk . $inglue . $tv;
      > }
      > return substr($return,strlen($outglue));
      > }
      > then translate it back in JavaScript... but I am afraid this could be
      > buggy or error prone... plus it doesn't handle multi-dimensional arrays.
      >
      > Thanks!
      >
      > -Jake
      >
    Your message has been successfully submitted and would be delivered to recipients shortly.