0

I'm sure this is a very easy thing but I couldn't find it in google for hours. I'm new to ActionScript and I'm trying to obtain an array of variables from a string that is generated by a .php file.

my php file outputs this:

var1=42&var2=6&var3=string

And my ActionScript code is:

public function CallAjax_VARIABLES(url:String , the_array:Array) 
{ 
 var request:URLRequest = new URLRequest(url); 
 var variables:URLLoader = new URLLoader(); 
 variables.dataFormat = URLLoaderDataFormat.VARIABLES; 
 variables.addEventListener(Event.COMPLETE, VARIABLES_Complete_Handler(the_array)); 
 try 
 { 
  variables.load(request); 
 }  
 catch (error:Error) 
 { 
  trace("Unable to load URL: " + error); 
 } 
} 

function VARIABLES_Complete_Handler(the_array:Array):Function {
  return function(event:Event):void {
  var loader:URLLoader = URLLoader(event.target); 
  //the_array = loader.data;   // this doesn't work.  
  //the_array = URLVariables.decode(loader); // this doesn't work either.
  //trace(loader.data['var1']); // this outputs 42, so I'm getting the string from php.
  };
}

I think you already understood this but, in the end, I want to have an array (In ActionScript) that will give me:

the_array['var1']=42;
the_array['var2']=6;
the_array['var3']="string";

What am I doing wrong? What should I do? Thanks!

EDIT: I'm trying to get variables FROM php TO ActionScript. e.g. My PHP file correctly converts the array to an html query, But I don't know how to parse them in an array in ActionScript.

void
  • 1,736
  • 6
  • 22
  • 28

3 Answers3

1

You should use URLVariables for this.

var vars:URLVariables = new URLVariables(e.target.data);

This way you can simply say:

trace(vars.var2); // 6

An array would be useless here as the result is associative rather than index based, though you can easily take all the values and throw them into an array with a simple loop:

var array:Array = [];
for(var i:String in vars)
{
    array.push(vars[i]);
}
Marty
  • 37,476
  • 18
  • 87
  • 159
0

Sorry, I thought this was a PHP question. In ActionScript, try this:

 var the_array:URLVariables = new URLVariables();
 the_array.decode(loader.data);

 trace(the_array.var1);
silkfire
  • 20,433
  • 12
  • 70
  • 93
  • Thanks, but my problem is in ActionScript side. I'm trying to parse variables from the string that is generated correctly by PHP. – void May 08 '13 at 13:50
  • Made an edit, try now. You where correct about URLVariables though. – silkfire May 08 '13 at 13:56
0

I think you are looking for parse_str function

parse_str($str, $output);
Maximus
  • 2,696
  • 4
  • 31
  • 51
  • Thanks, but my problem is in ActionScript side. I'm trying to parse variables from the string that is generated correctly by PHP. – void May 08 '13 at 13:51