Computing.Net > Forums > Programming > PHP: File into array problem

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

PHP: File into array problem

Reply to Message Icon

Name: jb60606
Date: June 1, 2007 at 03:42:13 Pacific
OS: Suse Linux
CPU/Ram: 1Ghz/512MB
Product: HP
Comment:

Why doesn't this work?

- I have a simple file (symbols.dat) containing one stock
symbol per line.

Example:

AMD
INTC
MSFT
AAPL

I want to take the contents of that file, put it into an array
and turn it into a single string, separated by "+".

Example:

$symbolfile = array("data/symbols.dat");
sort($symbolfile);
$symbols = implode('+', $symbolfile);

The final result(string) should like:

AMD+MSFT+INTC+AAPL+

If I skip the file, and just declare the array in the actual
php script, everything works fine. When I try and pull it
from the file, nothing is returned... not even an error.

I'm sure the proper permissions are in place and the file
definately exists. I tried opening the file in advance, using
fopen, though that didn't seem to make a difference
either.

Any ideas? Thanks in advance.



Sponsored Link
Ads by Google

Response Number 1
Name: jb60606
Date: June 1, 2007 at 04:03:26 Pacific
Reply:

correction, instead of:

$symbolfile = array("data/symbols.dat");

I'm using:

$symbolfile = file("data/symbols.dat");

Although, if I echo it out, it appears to work ok. It must be
a problem with another part of my script. I'm using the
string in a URL

http://www.example.csv$symbol

Again, when I declare the entire array and its contents
within the script, it works fine.


0

Response Number 2
Name: jb60606
Date: June 1, 2007 at 07:14:27 Pacific
Reply:

I've managed a workaround by simply separating each symbol by commas, instead of new lines.

However, now it appears I've lost the ability to sort the array.


0

Response Number 3
Name: jb60606
Date: June 4, 2007 at 07:37:17 Pacific
Reply:

This was resolved over at phpfreaks.com.

It turns out that there was in fact a space between each symbol and "+" marker; I assume from the file.

ex.

AMD +IBM +SIRI +MSFT +DELL

The code below took care of it.

$arr = file('data/symbols.dat');
sort($arr);
foreach($arr as $k=>$v){
$arr[$k]=trim($v);
}
$sym = implode('+', $arr);


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: PHP: File into array problem

Print multiple files into array www.computing.net/answers/programming/print-multiple-files-into-array/18284.html

c++ read file into array www.computing.net/answers/programming/c-read-file-into-array/9962.html

C arrays of strings from a file www.computing.net/answers/programming/c-arrays-of-strings-from-a-file/14025.html