PDA

View Full Version : Array Sort and Foreach


gorilla1
12-15-2002, 12:26 AM
I am trying to sort the records in a file, then index into the array, with the following code:

$data = file('xxx.txt');

$data = asort($data);

foreach($data as $key=>$element) {

This code fails with this message:
Invalid argument supplied for foreach()

That makes it sound like the sort left me without an array??
Note that if I leave out the line with the sort in it, the code works fine.

G

mordred
12-15-2002, 01:02 AM
The return type of asort() is void, so in your example $data is void rather than an array. Try to use

asort($data);

and tell us if that's helping in your situation.

gorilla1
12-15-2002, 01:33 AM
Yes, thqt did the trick. Thanks, Mordred.

G