Dryden Long
Dryden Long

Reputation: 10182

Trouble with sort() functions in PHP

I am trying to sort an array I have in PHP and for some reason all I get in return is "1" when I attempt to use any of the sort() functions. Here is the code I have so far:

$files = glob('Some\Random\Directory\*.txt');
$tag = array();
foreach($files as $file){
$fh = fopen($file, 'rb');
while($col = fgetcsv($fh)) {
if (isset($tag[$col[2]])) {
   $tag[$col[2]]++;}
else {
   $tag[$col[2]] = 1;}}
fclose($fh);}
print_r($tag);

Which results in displaying my array as expected. However, when I attempt to do:

echo arsort($tag);

All I get in return on the page is "1."

Any thoughts on what I am doing wrong? Thanks!

Upvotes: 0

Views: 63

Answers (1)

Jason McCreary
Jason McCreary

Reputation: 72991

Array sort() functions work on a reference of the array. As such they don't return the sorted array but true (1) or false (0).

print_r($tag);
arsort($tag);
print_r($tag);

Upvotes: 3

Related Questions