Difference in Codeigniter


I am working with the Api rest service in Codeigniter, my query is:

I have a json that returns all the students of the mini system1, in the mini system 2 I have a table called Assign where I keep the id of the student and other data, my problem is in making the query, that after saving a student to assign ( id_alumno ) show me the students that are not that table This is the code I was using:

  $datos=array_values(array_diff_key($jsonAlumno, $jsonAsignar));

this only compares me the id(alumno) with the id(Asignar) but I should compare id(Alumno) id_alumno(Asignar) with this would make the difference and I would get the ones that are not in the table Assign but it's not working for me obviously, and I'm re lost. The data is like this

Table Student in the system1 that I bring with the json Table Assign in the system2  the result that I want to obtain are the students that are not in the table Assign     public function compare () {

  $id = $this->session->userdata('usuario_id'); 
  $usuario = $this->Model_Usuario->find($id);
  $escuela= $usuario->escuela_id;

$curl = new Curl();

        $jsonA = $curl->response;       
        $jsonAlumno=json_decode(json_encode($jsonA), true);   

        $jsonAsignar= $this->Model_Asignar->all();      
        $long = count($jsonAsignar);                 

        $datos=array_values(array_diff_key($jsonAlumno, $jsonAsignar));

        $jsonDatos=json_decode(json_encode($datos), true);   

        foreach ($jsonDatos as $key => $value) {
            if ($value['escuela_id']==$escuela){                                    

            $datos1[] = array('id' => $value['id'],
                              'persona_name' => $value['persona_name'],
                              'persona_dni' => $value['persona_dni'],
                              'persona_cuil' => $value['persona_cuil'],
                              'curso_name' => $value['curso_name'] );


          echo ($resultado);  



Could you help me please that this has me head on and thanks in advance.

asked by Pao 04.09.2018 в 00:23

1 answer


instead of using array_diff_key that verifies by the key of the array you should use array_udiff that gives you the possibility to verify by any value of the array of objects.

// declaramos esta funcion
function comparar_id_alumnos($alumno, $asignar)
    // si retorna 0 es q existe, otro valor es q no existe
    return $alumno['id'] - $asignar['id_alumno'];


// esto deberia darte la diferencia
$ret = array_udiff($jsonAlumno, $jsonAsignar, 'comparar_id_alumnos');
var_dump($ret); // solo imprime el resultado

the array_udiff will remove from the $ jsonAlumno the elments that match the elements of $ jsonAsignar, verifying the id and the student_id respectively, and this difference will return it in $ ret.

answered by 04.09.2018 в 17:00