0
<?php

// Open the file
$filename = 'pvemail.txt';
$fp = fopen($filename, 'r'); 
// Add each line to an array
if ($fp) {
   $array = explode("\n", fread($fp, filesize($filename)));
}
//print_r ($array);
for ($c = 0; $c < count($array); $c++){
    $cell = explode(",", $array[$c]);
//print_r ($cell);

if ($cell[3] == 'ACTIVE')
    {
    echo $cell[2].' - '.$cell[12].' '.$cell[11];
    echo '<br/>';
    }
}
?>

This code takes a CSV file, reads it and the first if statement explodes each line of the file into its own array. The for loop the explodes each of these lines into arrays made up of 20 key=>value pairs. The second if statement then loops through these pairs to extract and print to the screen the three pairs that I want.
The problem I'm having is I now want the data to be sorted alphabetically, and so far no matter what sort() function I use, or where I put it, nothing happens.
I tried to use sort($cell) between the for loop and the second if loop and nothing happened, I tried using the same code inside, and after, the if loop and nothing happened, I've tried moving the $cell array to another array and sorting that, I've tried ksort($cell, asort($cell), I've even tried passing the array to JavaScript and sorting it that way and nothing is happening!
I'm starting to pull my hair out here, is there anything obvious that I'm missing or not doing that's stopping the sort working?

  • Would you not need to sort the $array instead of $cell? –  Sep 13 '13 at 15:31
  • I thought I'd need to sort $cell because that's the array I've exploded $array into? Sorry I'm a novice so I'm learning on the job and getting frustrated! –  Sep 13 '13 at 15:34
  • From the look of your code, it looks like you are taking single rows from the array into $cell. So you'd have to sort the whole array first before going in. –  Sep 13 '13 at 15:38

1 Answers1

0

It's not meaningful to sort $cell because it represents one row, not the whole file.

But let's start by simplifying the code. This line will single-handedly load your data and convert it into a multidimensional array:

$data = array_map('str_getcsv', file('pvemail.txt'));

You can then use usort with a callback function to sort this array of lines based on any column you want ("alphabetically" is not very clear). This question shows how to do it (disclosure: I have an answer there too).

Assuming that you want to sort based on the first column, you might do

usort($data, function($a, $b) { return strcmp($a[0], $b[0]); });

That should be it, you can then use foreach to print out the results.

Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
  • unfortunately as part of my apprenticeship I need to be using the explode() function, which is why my code is written the way it is. Unfortunately I am learning on the job here, so I'm a bit of a novice. –  Sep 13 '13 at 15:40
  • Is there any way to use a sort function within the code that I have? –  Sep 13 '13 at 15:40
  • @JadeLouiseWhelan: you would sort $array, not $cell. Use the code I give above. I don't get why you need to use explode; it's a very simple function, and in this case it's inadequate for any but the simplest input. – Jon Sep 13 '13 at 16:04