Go through a JSON array and display certain PHP data

1

A JSON is answering the following:

[
 {
 emple_cedula: "7913",
 emple_nombre: "PEPITO",
 emple_apellido: "PEREZ",
 resultado: "60"
 },
 {
 emple_cedula: "7913",
 emple_nombre: "PEPITO",
 emple_apellido: "PEREZ",
 resultado: "50"
 },
 {
 emple_cedula: "987654",
 emple_nombre: "VANESSA",
 emple_apellido: "PAZ",
 resultado: "100"
 },
 {
 emple_cedula: "987654",
 emple_nombre: "VANESSA",
 emple_apellido: "PAZ",
 resultado: "73"
 },
]

How could I make php show me a single name with all the results assigned to it?

Example:

7913 PEPITO PEREZ 60 50 <-(Estos son los resultados)
987654 VANESSA PAZ 100 73 <-(Estos son los resultados)

PHP CODE

if($response)
{   
    $personal = array();            
    foreach ($response as $respuesta)
    {
        if (!in_array(array($respuesta->emple_cedula, $respuesta->emple_nombre,$respuesta->emple_apellido), $personal)) {
            $personal[] = array($respuesta->emple_cedula, $respuesta->emple_nombre,$respuesta->emple_apellido,);                    
        }               
    }

    $datos['personal'] = $personal;
    echo json_encode($datos);
}   

With this code, when I see that I'm printing echo json_encode ($ data), it sends me this:

 7913 PEPITO PEREZ 
    987654 VANESSA PAZ 
  

But then what should I do in my php code to print it?

7913 PEPITO PEREZ 60 50 
    987654 VANESSA PAZ 100 73 

?

I would appreciate the interest.

    
asked by JDavid 20.04.2017 в 18:03
source

1 answer

3

Your query to the database seems to be of the Join type. Can you modify your query to the database? I think it's better to process the data from mysql than to use php (which is slower and you need to generate a map) to go through all the values.

SELECT  p.emple_cedula, p.emple_nombre, p.emple_apellido, GROUP_CONCAT(v.resultado) as resultados
FROM    persona p
JOIN    valores v
ON      v.emple_cedula = p.emple_cedula
GROUP BY
        p.emple_cedula

See the full answer

The main idea of changing the query is to return it from the common JOIN query that remains:

cédula    nombre       apellido      valor
7913      PEPITO       PEREZ         60
7913      PEPITO       PEREZ         50 
987654    VANESSA      PAZ           100
987654    VANESSA      PAZ           73 

To the way in which the values for each person are separated by comma in a way similar to this:

cédula    nombre       apellido      valores
7913      PEPITO       PEREZ         60, 50 
987654    VANESSA      PAZ           100, 73 

Then if you still want the array you use explode(',', $myString) to convert it from the value by commas to an array, or you can send it that way and the client will process it.

If you do not want to modify your query or the comment is not adapted. You must use an array within the foreach. Your array stores the emple_cedula as an index of the array that keeps the data sorted (this is an example):

$personal = array();            
foreach ($response as $respuesta)
{
    if(!isset($personal[$respuesta->emple_cedula]){
        $personal[$respuesta->emple_cedula]['emple_cedula'] = $respuesta->emple_cedula;
        $personal[$respuesta->emple_cedula]['emple_nombre'] = $respuesta->emple_nombre;
        $personal[$respuesta->emple_cedula]['emple_apellido'] = $respuesta->emple_apellido;
        $personal[$respuesta->emple_cedula]['resultado'] = array();
    }

    $personal[$respuesta->emple_cedula]['resultado'][] = $respuesta->resultado;
}

$datos['personal'] = $personal;
echo json_encode($datos);

This is the idea, it may have errors, but basically you use the user id as a key to index the results. This is better than using in_array which is a sequential search.

    
answered by 20.04.2017 / 20:02
source