我如何可以通过从PHP为ActionScript 3 /闪存阵列? [英] How can I pass an array from PHP to Actionscript 3/Flash?
问题描述
下面是我在PHP:
for ($i = 0; $i < 355; $i++)
{
echo "vote";
echo "$i";
echo "=$votesArray[$i]";
if($i != 354)
{
echo "&";
}
}
这应该将数据发送给Flash,看起来像vote0 = 2及vote1 = 5和......等等
Which should send data to Flash that looks something like "vote0=2&vote1=5&..." and so on.
下面是ActionScript 3方:
Here is the Actionscript 3 side:
var i:int;
for (i = 0; i < 355; i++)
{
var tempString:String = "vote" + i;
voteResults[i] = event.target.data.tempString;
}
我还没有尝试运行这个还没有,但我得到它不会工作的感觉。你可以看到我想要知道的有关系吗?对于每个迭代循环,我想它从event.target.data的不同部分取数据。对于第一次迭代,应当event.target.data.vote0。第二,event.target.data.vote1,等等。
I haven't attempted to run this yet, but I get the feeling it won't work. Can you see what I'm trying to get at, though? For each iteration of the for loop, I would like it to take the data from a different part of event.target.data. For the first iteration, it should be event.target.data.vote0. Second, event.target.data.vote1, and so on.
推荐答案
我会用XML这样的事情,但它可以通过URL连接codeD字符串来完成。
I would use xml for something like this, but it can be done with url encoded strings.
假设你加载了数据URLLoader和指定的DATAFORMAT为<一个href="http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/net/URLLoaderDataFormat.html"相对=nofollow> URLLoaderDataFormat.VARIABLES 中,你靠近。
Assuming you loaded your data with URLLoader and specified the dataFormat as URLLoaderDataFormat.VARIABLES, you are close.
如果你有一个原始字符串,你应该先分析其分解为名称/值对。这是的URLVariables 一样。
If you have a raw string, you should parse it first to break it down to name/value pairs. This is what URLVariables does.
总之,一旦你有一个包含名称/值的对象,你可以这样做:
Anyway, once you have an object containing names/values, you can do:
var i:int;
for (i = 0; i < 355; i++)
{
var tempString:String = "vote" + i;
voteResults[i] = event.target.data[tempString];
}
如果您使用点访问,将采取tempString的字面。如果使用托架访问,可变tempString的值进行评估。
If you use dot access, it will take "tempString" literally. If you use bracket access, the value of the variable tempString will be evaluated.
PS:顺便说一句,我不认为你的PHP会做你想做的。一个清洁的方式,IMO是:
PS: By the way, I don't think your php will do what you want. A cleaner way, IMO would be:
$pairs = array();
for ($i = 0; $i < 355; $i++)
{
$pairs[] = 'vote' . $i . '=' . $votesArray[$i];
// you might want to use rawurlencode($votesArray[$i]) to be safe if these are not numeric values.
}
echo implode('&',$pairs);
PS 2:此外,这是相当脆弱,因为你硬编码355如果你改变你的PHP,你必须改变你的AS code为好。你可以尝试这样的:
PS 2: Also, this is rather brittle since you're hardcoding 355. If you ever change your php, you'll have to change your AS code as well. You could try something like this:
var data:URLVariables = event.target.data as URLVariables;
for(var fieldName:String in data) {
var index:int = parseInt(fieldName.substr(4));
// or optionally, add an underscore to the name: vote_1
// so you can change the above to something like
// fieldName.split("_")[1]
voteResults[index] = data[fieldName];
}
或者,正如我以前,你可以使用XML,这是很简单和一个更好的选择,这个说,我想。
Or, as I said before, you could use xml, which is simple enough and a better option for this, I think.
这篇关于我如何可以通过从PHP为ActionScript 3 /闪存阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!