look for repeated values and put if it is the first, the second or the third time that is repeated

0

I hope you can help me I have an array in which I keep numbers that correspond to a specific subject ie they are the id, but I need to go through that array and verify that if the subject is repeated it means that it is the second time that is in the array and so if it comes out a third time is the third for example: mi_array = [1,4,6,7,9,4,1,8,3,4] go through it and print

1=primera
4=primera
6=primera
7=primera
4=segunda
1=segunda
8=primera
3=primera
4=tercera

I hope my explanation is understandable and you can help me

$pdo = new DB();
    $no_mat = array();
    $query = $pdo->connect()->prepare('SELECT ID_ESTUDIANTE,ID_CARRERA,ID_MATERIA FROM 'calificaciones' WHERE ID_ESTUDIANTE = ?');
    $query1 = $pdo->connect()->prepare('SELECT DISTINCT ID_ESTUDIANTE FROM 'calificaciones'');
    $query2 = $pdo->connect()->prepare('UPDATE calificaciones SET 'NO_MATRICULA' = ? WHERE ID_ESTUDIANTE = ? AND ID_MATERIA = ?');

    $query1->execute();
    foreach($query1 as $ids){
        $query->execute([$ids[0]]);
            foreach($query as $e){
            array_push($no_mat,$e[2]);//ESTE ARREGLO GUARDA EL ID DE LA MATERIA
        }
    //EL PROBLEMA ES QUE SI UNA MATERIA SE REPITE 3 VECES EN LA POSICION QUE ESTE ME SALE 3 Y NECESITO QUE EN LA PRIMERA SALGA 1 EN LA SEGUNDA 2 Y PUES CLARO EN LA TERCERA 3
        for($i=0;$i<count($no_mat);$i++){
            $cont=(count(array_keys($no_mat, $no_mat[$i])));
            if($cont>1 && $cont<3){
                $query2->execute([$cont,$ids[0],$no_mat[$i]]);
            }elseif($cont>2){
                $query2->execute([$cont,$ids[0],$no_mat[$i]]);
            }else{
                $query2->execute([1,$ids[0],$no_mat[$i]]);
            }   
        }
        unset($no_mat);
        $no_mat = array();
    }
    
asked by Tecno Gamer 05.01.2019 в 00:51
source

1 answer

0

// I ACHIEVED TO SOLVE MY PROBLEM WITH THIS CODE // I PUBLISH IT IF SOMEBODY IS SERVED OR HAS A SIMILAR PROBLEM // ANYWAY, THANK YOU FOR YOUR COMMENTS

<?php
require('controladores/conect.php');
$pdo = new DB();
$no_mat = array();
$query = $pdo->connect()->prepare('SELECT ID_ESTUDIANTE,ID_CARRERA,ID_MATERIA FROM 'calificaciones' WHERE ID_ESTUDIANTE = ?');
$query1 = $pdo->connect()->prepare('SELECT DISTINCT ID_ESTUDIANTE FROM 'calificaciones'');
$query2 = $pdo->connect()->prepare('UPDATE calificaciones SET 'NO_MATRICULA' = ? WHERE ID_ESTUDIANTE = ? AND ID_MATERIA = ? AND NUMERACION = ?');

$query3 = $pdo->connect()->prepare('UPDATE calificaciones SET 'NUMERACION' = ? WHERE ID_ESTUDIANTE = ? AND ID_CAL = ?');


$ac=1;
$j=0;
$query1->execute();
foreach($query1 as $ids){

        $query->execute([$ids[0]]);
            foreach($query as $e){
                array_push($no_mat,$e[2]);
            }
        $unicos = array_unique($no_mat);

        foreach($unicos as $id){
            foreach($no_mat as $idss){
                if($id == $idss){
                    $query2->execute([$ac,$ids[0],$id,$j]);
                    $ac++;
                }
                $j++;
            }
            $j=0;
            $ac=1;
        }
    $j=0;
    $ac=1;

    unset($no_mat);
    $no_mat = array();
    $unicos = '';
}
?>
    
answered by 13.01.2019 / 18:48
source