PHP-Reorder Array

0

I have developed an algorithm in php to reorder an array that I have, the code is as follows:

<?php
    $idescuelas=[4,5,8];

    $newalumnos=[];

    $sql="SELECT * FROM pruebas_alumno inner JOIN alumnos on pruebas_alumno.id_alumnos=alumnos.id_alumnos inner join escuelas on alumnos.id_escuelas=escuelas.id_escuelas WHERE id_competencias=1 and alumnos.id_sexo=2 and alumnos.id_categorias=1 and id_pruebas=2";
    $statement=$conexion->prepare($sql);
    $statement->execute();
    while($renglon=$statement->fetch(PDO::FETCH_ASSOC)){
        $alumnos[]=$renglon;
    }


    while(count(alumnos)>0){
        foreach ($idescuelas as $k) {
            for($l=count($alumnos)-1;$l>=0;$l--) {
                if($alumnos[$l]['id_escuelas']==$k){
                    $newalumnos[] =$alumnos[$l];
                    unset($alumnos[$l]);
                    echo count($alumnos).'<br>';
                    continue 3;
                }
            }
        }           
    }

    echo "--------------------<br>Nuevo array<br>";

    foreach ($newalumnos as $k) {
        echo $k['id_escuelas']. '<br>';
    }




?>

What the query throws at me is:

Finally what I want to achieve is that the new array be ordered by the id_schools as follows:

4
5
8
4
5
8

Apparently the algorithm works because it starts doing it but it does not end (I have removed the time limit in php.ini and it remains in an infinite blucle):

34
33
32
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11

Fatal error: Maximum execution time of 30 seconds exceeded in C:\AppServ\www\swiman\test2.php on line 33

Removing the while effectively does it ... but only once as they imagined:

34
33
32
--------------------
Nuevo array
4
5
8

I would appreciate your help. Greetings!

    
asked by Jess182 09.05.2018 в 16:45
source

1 answer

1

Create two arrays, the schools and one simulating the results of the test, and doing this you achieve what you want:

$arraySch = array(4, 5, 8);

$arrTest = array(
    0 => array('id_test'=>1, 'id_escuela' => 4,'id_sexo' => 1,'status' => 0 ),
    1 => array('id_test'=>2, 'id_escuela' => 4,'id_sexo' => 1,'status' => 0 ),
    2 => array('id_test'=>3, 'id_escuela' => 4,'id_sexo' => 1,'status' => 0 ),
    3 => array('id_test'=>4, 'id_escuela' => 4,'id_sexo' => 1,'status' => 0 ),
    4 => array('id_test'=>5, 'id_escuela' => 4,'id_sexo' => 1,'status' => 0 ),
    5 => array('id_test'=>6, 'id_escuela' => 4,'id_sexo' => 1,'status' => 0 ),
    6 => array('id_test'=>7, 'id_escuela' => 4,'id_sexo' => 1,'status' => 0 ),
    7 => array('id_test'=>8, 'id_escuela' => 4,'id_sexo' => 1,'status' => 0 ),
    8 => array('id_test'=>9, 'id_escuela' => 4,'id_sexo' => 1,'status' => 0 ),
    9 => array('id_test'=>10, 'id_escuela' => 4,'id_sexo' => 1,'status' => 0 ),
    10 => array('id_test'=>11, 'id_escuela' => 4,'id_sexo' => 1,'status' => 0 ),
    11 => array('id_test'=>12, 'id_escuela' => 4,'id_sexo' => 1,'status' => 0 ),
    12 => array('id_test'=>13, 'id_escuela' => 4,'id_sexo' => 1,'status' => 0 ),

    13 => array('id_test'=>14, 'id_escuela' => 5,'id_sexo' => 1,'status' => 0 ),
    14 => array('id_test'=>15, 'id_escuela' => 5,'id_sexo' => 1,'status' => 0 ),
    15 => array('id_test'=>16, 'id_escuela' => 5,'id_sexo' => 1,'status' => 0 ),
    16 => array('id_test'=>17, 'id_escuela' => 5,'id_sexo' => 1,'status' => 0 ),
    17 => array('id_test'=>18, 'id_escuela' => 5,'id_sexo' => 1,'status' => 0 ),
    18 => array('id_test'=>19, 'id_escuela' => 5,'id_sexo' => 1,'status' => 0 ),
    19 => array('id_test'=>20, 'id_escuela' => 5,'id_sexo' => 1,'status' => 0 ),
    20 => array('id_test'=>21, 'id_escuela' => 5,'id_sexo' => 1,'status' => 0 ),
    21 => array('id_test'=>22, 'id_escuela' => 5,'id_sexo' => 1,'status' => 0 ),
    22 => array('id_test'=>23, 'id_escuela' => 5,'id_sexo' => 1,'status' => 0 ),
    23 => array('id_test'=>34, 'id_escuela' => 5,'id_sexo' => 1,'status' => 0 ),
    24 => array('id_test'=>25, 'id_escuela' => 5,'id_sexo' => 1,'status' => 0 ),
    25 => array('id_test'=>26, 'id_escuela' => 5,'id_sexo' => 1,'status' => 0 ),

    26 => array('id_test'=>27, 'id_escuela' => 8,'id_sexo' => 1,'status' => 0 ),
    27 => array('id_test'=>28, 'id_escuela' => 8,'id_sexo' => 1,'status' => 0 ),
    28 => array('id_test'=>29, 'id_escuela' => 8,'id_sexo' => 1,'status' => 0 ),
    29 => array('id_test'=>30, 'id_escuela' => 8,'id_sexo' => 1,'status' => 0 ),
    30 => array('id_test'=>31, 'id_escuela' => 8,'id_sexo' => 1,'status' => 0 ),
    31 => array('id_test'=>32, 'id_escuela' => 8,'id_sexo' => 1,'status' => 0 ),
    32 => array('id_test'=>33, 'id_escuela' => 8,'id_sexo' => 1,'status' => 0 ),
    33 => array('id_test'=>34, 'id_escuela' => 8,'id_sexo' => 1,'status' => 0 ),
    34 => array('id_test'=>35, 'id_escuela' => 8,'id_sexo' => 1,'status' => 0 ),
    35 => array('id_test'=>36, 'id_escuela' => 8,'id_sexo' => 1,'status' => 0 ),
    36 => array('id_test'=>37, 'id_escuela' => 8,'id_sexo' => 1,'status' => 0 ),
    37 => array('id_test'=>38, 'id_escuela' => 8,'id_sexo' => 1,'status' => 0 ),
    38 => array('id_test'=>39, 'id_escuela' => 8,'id_sexo' => 1,'status' => 0 ),
);

$control = 0;
$newArray = array();
$num = count( $arrTest );

// Voy a buscar tantas veces como test haya
for( $i = 0; $i < $num; $i++ )
{
    // Establescon un control para desplazarme por el array de escuelas uno a uno y repetir
    if( $control == ( count( $arraySch ) ) )
    {
        $control = 0;
    }
    // En cada uno de los test busco, si encuentro, tomo el array y lo quito para que no esté en la siguiente busqueda
    foreach( $arrTest as $key=>$sco )
    {
        if( $arraySch[ $control ] == $sco['id_escuela'] )
        {
            // El ordenamiento esta definido ya por el $control
            $newArray[ $sco['id_test'] ] = $arrTest[$key];
            unset( $arrTest[$key] );
            break;
        }
    }

    $control++;

}

echo '<pre>'.print_r( $newArray, true ).'</pre>';
    
answered by 09.05.2018 в 18:21