PDA

View Full Version : PHP Array Question



jw06
November 29th, 2004, 10:50 AM
Hmmm, I was wondering if it is possible to use a file as an array list.. Im wondering this because I am thinking bout making a random image thing, like the tutorial on kirupa. But instead of putting all my file names in there and editing that file everytime, is there a way to make a .txt file or something go into it? If it doesnt make sence let me know :S Ill reword it or something... Thanks..

andr.in
November 29th, 2004, 10:56 AM
If I understand it right you're trying to get the array from a text file right?
Simple

$data = file_get_contents("yourfile");
$array = split("|", $data);
replace "yourfile" withe the file name.
And in the next line just split it into an array whenevr it encounters "|". You cna change that symbol to whatever u want
if your array file is: "foo|bar|foo" and u split that then you'd get "foo", "bar" and "foo" as the array elements

jw06
November 29th, 2004, 11:01 AM
hmmm , yes that is what i want, but i dont quite understand the "|" say there is a new file located on each line.. How would I split it for each line:?.

Thanks

andr.in
November 29th, 2004, 11:12 AM
the separate line thing may work or it may not... I've had problems with it...
But basically it should just be

$array = split("\n", $data);

\n is the line break symbol for text files... if that doesn't work then u could try \r ...

jw06
November 29th, 2004, 11:20 AM
:D thanks