使用PHP过滤CSV

Hi I have a CSV file that needs filtering, from A-C, D-F and so on

    Aaron, Male, aaron@website.com
    Arianne, Female, arianne@something.com
    Bea, Female, bea@hello.com
    Carlos, Male, carlos@website.com
    Drake, Male, drake@website.com
    Delilah, Female, del@hello.com
    Erica, Female, erika@webisite.com
    Flint, Male, flint@something.com

Im not good at PHP and I tried to fix it by doing this code below.

<?php
$file  = fopen('LoggedInUsers.csv', 'r');

$first = array('Aaron');
$last = array('Carlos');



while (($line = fgetcsv($file)) !== FALSE) {  
    list($name, $log, $department) = $line;

    $firstt = array('Aaron');
    if ($first < 0) {
    }
    $last = array ('Carlos');
    if ($last < 0) {
}
?>

What I would do is create a pseudo grouping structure first (an array) A-C, D-F, ... that would be used for grouping, then just check the current first letter on the iteration, then just push it inside according to its belonged format. Example:

$filters = array_chunk(range('a', 'z'), 3); // grouping structure
$file  = fopen('LoggedInUsers.csv', 'r');

$data = array();
while (($line = fgetcsv($file)) !== false) {
    list($name, $log, $department) = $line;
    foreach($filters as $batch =>$filter_batch) {
        // if the current name does belong to this structure, push it inside
        if(in_array(strtolower(substr($name, 0, 1)), $filter_batch)) {
            $data[$batch][] = $name;
        }
    }
}

echo '<pre>';
print_r($data);

So the output should be:

Array
(
    [0] => Array // batch of A-C
    (
        [0] => Aaron
        [1] => Arianne
        [2] => Bea
        [3] => Carlos
    )

    [1] => Array // batch of D-F
    (
        [0] => Drake
        [1] => Delilah
        [2] => Erica
        [3] => Flint
    )

) 

You can use substr to get a specific character from your string, but there's a specific method to compare the first n characters of a string called strncasecmp

First of all, can you guarantee that the records in the CSV are sorted alphabetically? I'm assuming not so we'll loop through every row and add the matching ones to an array;

$first = "A";
$last = "C";

$Ret = array();

while (($line = fgetcsv($file)) !== FALSE) {  
    list($name, $log, $department) = $line;

    if (strncasecmp($name, $first, 1) >= 0 && strncasecmp($name, $last, 1) <= 0) {
        array_push($Ret, array($name, $log, $department));
    }
}

//Show what we got
print_r($Ret);

After running this, $Ret should contain all names starting with letters from $first to $last (inclusive)

This has the added bonus that you don't need to know a specific name, just the letters you're interested in.

Note that strncasecmp is used for a case-insensitive string comparison. Basically it returns 0 if the first n characters of the strings are equal, a negative value if the first string is "before" the second, or a positive value if it's "after".

/**
 * Function to sort multidimensional array on first element using 
 * PHP's usort().
 * @see http://uk1.php.net/manual/en/function.usort.php
 **/
function username_sort( $foo, $bar ){
    return ( $foo[0] > $bar[0] );
}

// Gets all file content into a multidimensional array in memory
$loggedInUsers = array_map( 'str_getcsv', file( 'LoggedInUsers.csv' ) );

// Sorts the array by the first element (username) in a copy of the data.
$sorted_loggedInUsers = usort( $loggedInUsers, 'username_sort' );

At this point you have the data in a sorted array, to be array_slice()d however you wish to do so.