How to classify a.csv file column without using PHP Sort functions?

Asked

Viewed 72 times

-1

I need your help to complete a college activity. I have to sort the rows of a.csv file according to one of the columns and display the result. I could use the array_multisort, but I need to write it through a function created by myself.
Note: I don’t want to change the original file, just manipulate it, without using PHP’s Sort functions. I want to create my own function to do this. The columns in my file are: Name;Name_mae;City_nasc
That’s what I want, but without using the array_multiosrt:

    <?php   
        $handle = fopen("Arquivo.csv", "r");
        $row = 0;
        while($line = fgetcsv($handle, 1000, ";")) {
            if ($row++ == 0) {
                continue;
            }
            $Arquivo[] = ['Nome' => $line[0],
            'Nome_mae' => $line[1], 'Cidade_nasc' => $line[2]];
        }

        $Matriz = $Arquivo;

        foreach ($Matriz as $key => $row){
            $Nome[$key] = $row['Nome'];
            $Nome_mae[$key] = $row['Nome_mae'];
            $Cidade_nasc[$key] = $row['Cidade_nasc'];
        }

        array_multisort($Nome, SORT_ASC, $Matriz);
        print_r($Matriz); #Imprime a Matriz por ordem alfabética dos Nomes

        foreach ($Matriz as $key => $row){
            $Nome[$key] = $row['Nome'];
            $Nome_mae[$key] = $row['Nome_mae'];
            $Cidade_nasc[$key] = $row['Cidade_nasc'];
        }

        array_multisort($Nome_mae, SORT_ASC, $Matriz);
        print_r($Matriz); #Imprime a Matriz por ordem alfabética dos nomes 
        das Mães

        foreach ($Matriz as $key => $row){
            $Nome[$key] = $row['Nome'];
            $Nome_mae[$key] = $row['Nome_mae'];
            $Cidade_nasc[$key] = $row['Cidade_nasc'];
        }

        array_multisort($Cidade_nasc, SORT_ASC, $Matriz);
        print_r($Matriz); #Imprime a Matriz por ordem alfabética das Cidades

        print_r($Arquivo); #Imprime a Matriz Original
    ?>

1 answer

0

I would create a key to $Arquivo[] with the value you would like to use as a reference and then use ksort() or krsort() to reorder.

For example:

$handle = fopen("Arquivo.csv", "r");

$header = fgetcsv($handle, 1000, ";");

while ($row = fgetcsv($handle, 1000, ";")) {
    $Arquivo[$row['data']] = array_combine($header, $row);
}

ksort($Arquivo);

Edit:

There are more elegant solutions, but the logic is more or less this:

$arr = [4, 2, 5, 1, 3];
function mySort(array $input): array
{
    $output = [];
    $count = count($input);
    for ($i = 0; $i < $count; $i++) {
        if ($i == 0) {
            // grava a primeira entrada direto.
            $output[] = $input[$i];
        } elseif ($output[0] > $input[$i]) {
            // se a entrada for menor que o primeiro valor no array,
            // adiciona a entrada no inicio.
            array_unshift($output, $input[$i]);
        } elseif (end($output) < $input[$i]) {
            // se for maior adiciona no final.
            $output[] = $input[$i];
        } else {
            // varre o array procurando a posição no meio
            for ($v = 0; $v < count($output); $v++) {
                if ($input[$i] < $output[$v] && 
                    key_exists($v + 1, $output) &&
                    $input[$i] < $output[$v + 1]) 
                    {
                    // divide o array em dois
                    $start = array_slice($output, 0, $v);
                    $end = array_slice($output, $v);
                    $start[] = $input[$i];
                    // monta o array novamente
                    $output = array_merge($start, $end);
                    break;
                }
            }
        }
    }
    return $output;
}

var_dump(mySort($arr));
// array (size=5)
//     0 => int 1
//     1 => int 2
//     2 => int 3
//     3 => int 4
//     4 => int 5
  • Legal Bruno Chaves, this is a good answer, however it does not solve my problem, because I need to order creating my own function, ie not using the functions of ordering the language itself. (type, uasort(), usort(), ksort()...)

  • I edited my answer with a possible solution.

  • Wow, Bruno Chaves. I analyzed your answer, and I couldn’t understand it very well, I don’t know if it’s really what I wanted, I’ll edit my question, and if it’s really what I wanted, then you explain to me, okay ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.