-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
?>
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()...)
– DANILO MIGUEL DE SOUZA
I edited my answer with a possible solution.
– Bruno Chaves
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 ?
– DANILO MIGUEL DE SOUZA