简体   繁体   English

如何从URL编码的字符串中获取数组?

[英]How to Get Array From URL Encoded String?

I'm sure this is a very easy thing but I couldn't find it in google for hours. 我敢肯定这是一件很容易的事,但是我已经有好几个小时在Google中找不到了。 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. 我是ActionScript的新手,正在尝试从.php文件生成的字符串中获取变量数组。

my php file outputs this: 我的PHP文件输出如下:

var1=42&var2=6&var3=string

And my ActionScript code is: 我的ActionScript代码是:

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: 我想您已经了解了这一点,但最后,我想拥有一个数组( 在ActionScript中 ),该数组将给我:

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. 编辑 :我正在尝试从php到ActionScript中获取变量。 eg 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. 例如,我的PHP文件将数组正确转换为html查询,但是我不知道如何在ActionScript中将其解析为数组。

You should use URLVariables for this. 您应该为此使用URLVariables

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]);
}

Sorry, I thought this was a PHP question. 抱歉,我认为这是一个PHP问题。 In ActionScript, try this: 在ActionScript中,尝试以下操作:

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

 trace(the_array.var1);

我认为您正在寻找parse_str函数

parse_str($str, $output);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM