# New Array From Php Array

**URL:** <https://forum.kirupa.com/t/new-array-from-php-array/262713>\
**Category:** flash\
**Created:** [June 9, 2008, 12:56pm UTC](https://forum.kirupa.com/t/new-array-from-php-array/262713 "2008-06-09T12:56:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![brndn](https://avatars.discourse-cdn.com/v4/letter/b/dec6dc/32.png) [@brndn](https://forum.kirupa.com/u/brndn)\
**Post date:** [June 9, 2008, 12:56pm UTC](https://forum.kirupa.com/t/new-array-from-php-array/262713/1 "2008-06-09T12:56:42Z")

</div>

Hi all  
I am have found a few examples on the net of how to load an array from a php generated script and then turn it into a flash array.

using this method

```auto
var thearray=new Array();
var recvars=new LoadVars();

recvars.onLoad=function(success){
     if(success){
         thearray=this.thestring.split("|");
        // thearray.push(recvars);
          trace(thearray);
           LOADTEXT.text = thearray;
           trace(thearray);
     }
};
recvars.load("http://mysite/returnarray.php");

```

to access this php file

```auto

<?php
$thearray=array('item0','item1','item2','item3');
$thestring=implode($thearray,','); //makes: item0|item1|item2|item3
echo "&thestring=$thestring"; 
?>

```

it seems to work ok except the fact that I can not call individual elements of the array??

like this

trace(thearray[1]);

so is it creating a well formed array???  
it spit out undefined.

---

<div class="post-metadata">

**Author:** ![brndn](https://avatars.discourse-cdn.com/v4/letter/b/dec6dc/32.png) [@brndn](https://forum.kirupa.com/u/brndn)\
**Post date:** [June 9, 2008, 12:58pm UTC](https://forum.kirupa.com/t/new-array-from-php-array/262713/2 "2008-06-09T12:58:35Z")

</div>

ok so trace(thearray[0]); spits out the entire elements.  
so I am presuming I will have to push each item into the array.  
but how do I loop through the total array echoed from php??

---

<div class="post-metadata">

**Author:** ![djheru](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/djheru/32/3190_2.png) [@djheru](https://forum.kirupa.com/u/djheru)\
**Post date:** [June 9, 2008, 1:15pm UTC](https://forum.kirupa.com/t/new-array-from-php-array/262713/3 "2008-06-09T13:15:07Z")

</div>

Your implode statement is wrong. The the array should be the second parameter. Also, you have set the “glue” to be a comma, not a pipe.

In the php section, I would send flash the number of elements that will be coming over like so:

```php

<?php
$thearray=array('item0','item1','item2','item3');
//
$numitems = count($thearray);
//
$thestring=implode('|',$thearray); //makes: item0|item1|item2|item3
echo "&numitems=$numitems&thestring=$thestring"; 
?>

```

Then, you could set up a for loop to add the elements to the AS array

---

<div class="post-metadata">

**Author:** ![brndn](https://avatars.discourse-cdn.com/v4/letter/b/dec6dc/32.png) [@brndn](https://forum.kirupa.com/u/brndn)\
**Post date:** [June 9, 2008, 1:19pm UTC](https://forum.kirupa.com/t/new-array-from-php-array/262713/4 "2008-06-09T13:19:26Z")

</div>

hey thanks for that.  
so my as loop would be like this??

---

<div class="post-metadata">

**Author:** ![brndn](https://avatars.discourse-cdn.com/v4/letter/b/dec6dc/32.png) [@brndn](https://forum.kirupa.com/u/brndn)\
**Post date:** [June 9, 2008, 1:21pm UTC](https://forum.kirupa.com/t/new-array-from-php-array/262713/5 "2008-06-09T13:21:15Z")

</div>

recvars.onLoad=function(success){  
if(success){

```
	 thearray=this.thestring.split("|");

```

for(i=0; i\<thearray.length; i++){  
theNEWarray.push(thearray);  
}  
LOADTEXT.text = thearray;  
trace(thearray[1]);  
}  
};

---

<div class="post-metadata">

**Author:** ![djheru](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/djheru/32/3190_2.png) [@djheru](https://forum.kirupa.com/u/djheru)\
**Post date:** [June 9, 2008, 1:26pm UTC](https://forum.kirupa.com/t/new-array-from-php-array/262713/6 "2008-06-09T13:26:24Z")

</div>

Sorry, can’t help with that part. My AS skill are akin to a mentally impaired lemur.

---

<div class="post-metadata">

**Author:** ![brndn](https://avatars.discourse-cdn.com/v4/letter/b/dec6dc/32.png) [@brndn](https://forum.kirupa.com/u/brndn)\
**Post date:** [June 9, 2008, 1:28pm UTC](https://forum.kirupa.com/t/new-array-from-php-array/262713/7 "2008-06-09T13:28:52Z")

</div>

no worries cheers for the help.

---

<div class="post-metadata">

**Author:** ![brndn](https://avatars.discourse-cdn.com/v4/letter/b/dec6dc/32.png) [@brndn](https://forum.kirupa.com/u/brndn)\
**Post date:** [June 9, 2008, 1:37pm UTC](https://forum.kirupa.com/t/new-array-from-php-array/262713/8 "2008-06-09T13:37:57Z")

</div>

I think there is no way to evaluate the items and the length in flash and then push them to a new array.  
otr at least I dont know the propper method.

---

<div class="post-metadata">

**Author:** ![brndn](https://avatars.discourse-cdn.com/v4/letter/b/dec6dc/32.png) [@brndn](https://forum.kirupa.com/u/brndn)\
**Post date:** [June 9, 2008, 2:23pm UTC](https://forum.kirupa.com/t/new-array-from-php-array/262713/9 "2008-06-09T14:23:38Z")

</div>

bump^

---

<div class="post-metadata">

**Author:** ![brndn](https://avatars.discourse-cdn.com/v4/letter/b/dec6dc/32.png) [@brndn](https://forum.kirupa.com/u/brndn)\
**Post date:** [June 9, 2008, 2:59pm UTC](https://forum.kirupa.com/t/new-array-from-php-array/262713/10 "2008-06-09T14:59:11Z")

</div>

var thearray=new Array();  
lv = new LoadVars();  
lv.onLoad = function() {  
st = this.thestring;  
count = st.split(",");  
c = count.length;  
for (z=0; z\<c; z++) {

trace(count[z]);  
thearray.push(count[z]);

}  
LOADTEXT.text = thearray[1];  
};  
lv.load("/returnarray.php");

works fine
