簡體   English   中英

如何從URL編碼的字符串中獲取數組?

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

我敢肯定這是一件很容易的事,但是我已經有好幾個小時在Google中找不到了。 我是ActionScript的新手,正在嘗試從.php文件生成的字符串中獲取變量數組。

我的PHP文件輸出如下:

var1=42&var2=6&var3=string

我的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.
  };
}

我想您已經了解了這一點,但最后,我想擁有一個數組( 在ActionScript中 ),該數組將給我:

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

我究竟做錯了什么? 我該怎么辦? 謝謝!

編輯 :我正在嘗試從php到ActionScript中獲取變量。 例如,我的PHP文件將數組正確轉換為html查詢,但是我不知道如何在ActionScript中將其解析為數組。

您應該為此使用URLVariables

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

這樣,您可以簡單地說:

trace(vars.var2); // 6

數組在這里是沒有用的,因為結果是關聯的而不是基於索引的,盡管您可以輕松地將所有值放入並通過簡單的循環將它們放入數組中:

var array:Array = [];
for(var i:String in vars)
{
    array.push(vars[i]);
}

抱歉,我認為這是一個PHP問題。 在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