The problem is that I have to enter name, id and age, so after reading the data I have to print it as a "table" from highest to lowest taking into account the age. I know that in php sort, rsort and other functions are used for sorting arrays, but I don't know how to do it when it's an array within another.
The idea is that the array that is in position 1 passes to position 0
What I expect to come out in this case is that the row containing "hello", "5", "87" remains in the place of the row containing "AAA" that is, if the age is greater, then it remains in the first position
<?php
session_start();
if ($_POST) {
if (isset($_POST['enviar'])) {
$_SESSION['personas'][]=["nombre" => $_POST['nombre'],
"cedula" => $_POST['cedula'],
"edad" => $_POST['edad']];
}
if(isset($_POST['eliminar'])){
unset($_SESSION['personas'][$_POST['ind']]);
}
foreach($_SESSION['personas'] as $ind => $persona){
?>
<form action="" method="post">
<input type="text" value = <?php echo $persona['nombre']?> name = "nombre" placeholder ="Nombre">
<input type="text" value= <?php echo $persona['cedula']?> name="cedula" placeholder="Cedula">
<input type="text" value= <?php echo $persona['edad']?> name="edad" placeholder="Edad">
<input type="hidden" value=<?php echo $ind?> name="ind">
<button name="eliminar">Eliminar</button>
</form>
<?php
}
echo '<pre>';
print_r($_SESSION);
}
?>
As you say, it should be used
usort
, whereusort
what it does is sort an array, here you can find the documentation .But in this case what you should do is seek to apply it to the Age inside your array and for that a function/callable is used:
Explaining what happens inside the function or callable:
What is expected is that the callable returns an integer:
-1, 0 ,1
in order to know if it is less, equal or greater and determine if the element should be moved or kept in the position of the array... then for this reason the operator is usedNave espacial <=>
that must return one of these integers fromphp 7