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.

1
  • Did my reply help you? Commented May 8, 2013 at 16:48

3 Answers 3

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]);
}
Sign up to request clarification or add additional context in comments.

Comments

0

I think you are looking for parse_str function

parse_str($str, $output);

1 Comment

Thanks, but my problem is in ActionScript side. I'm trying to parse variables from the string that is generated correctly by PHP.
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);

2 Comments

Thanks, but my problem is in ActionScript side. I'm trying to parse variables from the string that is generated correctly by PHP.
Made an edit, try now. You where correct about URLVariables though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.