- Home /
Get array from a php, put it into an array in Unity?
This is my php script. I'm trying to make it get all songs in that folder on my website, then send it to unity, so unity can make a playlist and stream the songs one by one, to save memory. I've barely done any php, how would I get the array from the php and put it into one in Unity? It's probably simple, but google doesn't help me much atm.
if ($handle = opendir('Playlist/')) {
echo "Directory handle: $handle\n";
echo "Entries:\n";
while (false !== ($entry = readdir($handle))) {
echo "$entry\n";
}
closedir($handle);
}
Answer by PouletFrit · Jun 14, 2014 at 01:54 PM
Sending data between php scripts and unity is normaly done using Get or Post Request via WWW or WWWForm classes. And you can't send an array using that method, however you could send CSV (comma-separeted values) representing an array.
Add a comma in between all of your "echos".
while (false !== ($entry = readdir($handle))) {
echo $entry . ",";
}
And then in Unity you can easily retreive that data using a simple string.Split(',') and convert it in an array
string url = "http://example.com/script.php";
WWW www = new WWW(url);
yield return www;
string[] songNames = www.text.Split(',');
Could you write this in javascript? Thanks a lot btw! Sucks you cant directly get it from an array, but this will do pretty much the same.
it would pretty much be the same I think... not really sure though
var url = "http://example.com/script.php";
var www : WWW = new WWW (url);
yield www;
var songnames = www.text.Split(",");
or something along thoses lines
I just got it converted, it doesn't work though. Here is how it is:
var getPlaylist : String = "http://shuleii.dk/Guardian/getfiles.php";
var songNames : String[];
function Start () {
var www : WWW = new WWW (getPlaylist);
yield www;
var songNames = www.text.Split(","[0]);
}
remove the [0] from Split
Split will return an array so if you want to access his first value use:
songNames[0]
if you wanted to only store the first value of the array that www.text.Split return in the variable songNames:
var songNames = www.text.Split(",")[0];
It wont allow me to erase the 0 >.<
I get the error Assets/[$$anonymous$$it] Scripts/Radio.js(15,32): BCE0023: No appropriate version of 'String.Split' for the argument list '(String)' was found.
Can I get you to write the script like I did above with the changes you mean? I'm either doing it wrong, or it doesn't work like that in Javascript? Thanks !! :>
Your answer
![](https://koobas.hobune.stream/wayback/20220613151520im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Array Dump to String for Send via WWW 1 Answer
wwwform send an array the right way! 0 Answers
Putting array nr. 1 in a string ;o? 2 Answers
About renting the host for the game 1 Answer
Unity - MySQL data loss 0 Answers