-1
        foreach($myFile as $value){
            $arr = explode(",",$value);

           echo "<tr>";
                foreach($arr as $out) {

                      if ((strcmp($arr[$city], $out)) == 0){
                        echo "<td><i>" . $out . "</i></td>";
                    }

                    else if((strcmp($arr[$pick], $out)) == 0){
                        $var = ucfirst(strtolower($out));
                        echo "<td>". $var. "</td>";
                        unset($var);
                    }

                    else{
                        echo "<td>" . $out . "</td>";
                    }
                    $count++;
                    unset($out);
                }
            echo "</tr>";
            unset($arr);
        }
        unset($myFile);

The code above is used to read a .csv file and print it as HTML Table. It's working already, but I'm having some troubles with sorting it.

The content of my .csv file is the variable $myFile. That's the output

Now I should sort the array by the column last, how can I do that?

And the other question: Why do I get the Error of Undefined Offset? The error lines are the if((strcmp(...)). The array is having 10 entries and the variable values are:
$pick = 9
$city = 5

Stöger
  • 155
  • 1
  • 11

2 Answers2

0

Use the php multisort function on the $arr array right after you define it.

foreach ($arr as $key => $row)
{
    $last[$key] = $row['last'];
}
array_multisort($last, SORT_DESC, $arr)
namelivia
  • 2,657
  • 1
  • 21
  • 24
0

You can accomplish this with usort. It takes a function with two arguments, which should be compared to each other. The function should return an int. As for your other error, I am guessing that you have this code in a function, which does not have access to a global variable.

Please note that trying to figure out if the column you are going to display is a special column by comparing the values of these columns is... weird. If you want to do something special in certain columns, just iterate with a for-loop instead of a foreach-loop, and just compare column indexes.

function displayTable( $myFile ) {
  global $columnCity, $columnPick;

  usort( $myFile, function( $a, $b ) {
    global $columnLast;
    return strcmp( $a[$columnLast], $b[$columnLast] );
  } );

  echo "<table>";

  foreach($myFile as $value) {
    $arr = explode( ",", $value );
    $count = 0; //Useless?

    echo "<tr>";

    for( $i = 0; $i < count( $arr ); $i++ ) {
      $out = $arr[$i];
      if( $i == $columnCity ) {
        echo "<td><i>{$out}</i></td>";
      } else if( $i == $columnPick ) {
        $var = ucfirst(strtolower($out));
        echo "<td>{$var}</td>";
        unset($var);
      } else {
        echo "<td>{$out}</td>";
      }
      $count++;
      unset($out);
    }

    echo "</tr>";
    unset($arr);
  }

  unset($myFile);

  echo "</table>";
}

displayTable( $input );
?>

A fiddle is available here: http://phpfiddle.org/lite/code/yqii-fkn2

Sumurai8
  • 20,333
  • 11
  • 66
  • 100