PDA

View Full Version : PHP > Flash Array


abdul_zu
08-04-2005, 07:03 AM
Hello,

I have little problem when i am retreving array from PHP array into Flash array.
here is my php file
<?
$vall[1] = 1;
$vall[2] = 2;
$vall[3] = 3;

$rString .= "&totd=".$vall.;
echo $rString;
?>

This is action script:

num = new Array();
num = this["totd"];
trace(num[2]); //Result: Undifined
trace(num); //Resul: Array

I hoped that i explained to find the problem.
Why i cannot get the value when i am trying trace using num[2], even this["totd"] is passing the array to num.

Thank You

seeFresh
08-04-2005, 07:51 AM
I'm not sure if there is a better way, but I know this will work.

<?
$vall[1] = 1;
$vall[2] = 2;
$vall[3] = 3;


$rString .= "&totd=";
//not sure of php syntax

for(var i = 0; i<$vall.length;i++) {
$rString .= "|" . $vall[i];
}


echo $rString;
?>

then in as use

num = new Array();
num = this.totd.split("|");

for(i = 1; i<num.length; i++) {
trace(i);
}


just remember that your first value is going to be blank, you can unshift if you want.

I know this is sloppy, but it works.


Chris
seeFresh.net

arianhojat
08-04-2005, 01:14 PM
to get of that '1st' blank value can't you do this...

$rString .= ( ($i==0) ? "" :"|") . $vall[i];

so uses conditional operator, basically an inline if statement, that if its the 1st value, dont add a "|", else add "|"



i wish you could do something like this in php to flash
..
$rString .= "&vall[i]=" . $vall[i];
..
echo "$rString"

where it would echo &vall[0]=100&vall[1]=101&vall[2]=102
thought i saw something like that for loadVars or was it my imagination?

destr3
08-04-2005, 02:24 PM
Hi abdul_zu,
unless you're using something like amfphp (http://www.amfphp.org/) , then you can't really pass data structures [like arrays] to flash. so you basically have to deconstruct them into a string, and then reconstruct them into an array in flash, kind of like what seeFresh was doing. so in php you would do something like this:


<?php
$vall = array("first","second","third");
echo "&myArray=" . implode("|",$vall);
?>


and in actionscript, to get it back into an array:


newArray = myArray.split("|");
for(var i=1; i<newArray.length; i++) {
trace(myArray[i]);
}


Hope this helps!

Xeef
08-04-2005, 02:27 PM
of course you can do this (hmmm or atleast i think so :p)

$rString = "";
for(var i = 0; i<count($vall);i++) {
$rString .= "vall[".i."]" . $vall[i];
}


but it will NOT by an array !!!
you will have variables whit he name "vall[0]" ...
to reach dem you will need something like _root["vall[0]"] :p


@seeFresh
is this PHP syntax ??? --> for(var i = 0; i<$vall.length;i++) { ???

$vall.length --> count($vall)

destr3
08-04-2005, 02:47 PM
of course you can do this (hmmm or atleast i think so :p)
$rString = "";
for(var i = 0; i<count($vall);i++) {
$rString .= "vall[".i."]" . $vall[i];
}

but it will NOT by an array !!!
you will have variables whit he name "vall[0]" ...
to reach dem you will need something like _root["vall[0]"] :p

hey Xeef, it will be an array in flash if you use myArray=val.split("|") in flash, if 'val' is a string from php that looks like this:
val=value1|value2|value3.

split will take the elements that are separated by "|" and return an array.


@seeFresh
is this PHP syntax ??? --> for(var i = 0; i<$vall.length;i++) { ???

$vall.length --> count($vall)
that's not php syntax, that's kind of a mix between php and actionscript,haha - he's just not using the dollar sign for his i variable. the php syntax is:


for ($i=0; $i<sizeof($vall); $i++){
//do stuff
}
...but you don't even need to use a for loop because you can just turn an array into a string in php by using implode().

Xeef
08-04-2005, 02:52 PM
you was posting when i was write my post :p

and yeh you idea is better i just was concentrated on PHP and wasn't wana do anithing in flash :p

sizeof
sizeof -- Alias of count()

abdul_zu
08-06-2005, 05:06 AM
Hi friends,

I used the logic of destr3, and really my problem is solved.
I am really happy with such kinds of yours help :)

Good Bye! :p