JSONArray eliminate duplication-

0

Given a JSONArray with several JSONObects inside with the following structure:

    [ 
      {"Id_temp":"1","temperatura":"20","Insertado_temp":"2016-08-16 12:30:29"},
      {"Id_press":"1","presion":"34","Insertado_press":"2016-08-16 16:18:36"},
      {"Id_press":"1","presion":"34","Insertado_press":"2016-08-16 16:18:36"},
      {"Id_temp":"1","temperatura":"20","Insertado_temp":"2016-08-16 12:30:29"},
      {"Id_temp":"2","temperatura":"25","Insertado_temp":"2016-08-16 15:48:53"},
      {"Id_press":"2","presion":"34","Insertado_press":"2016-08-16 16:18:36"},
      {"Id_temp":"4","temperatura":"50","Insertado_temp":"2016-08-16 18:17:33"},
      {"Id_temp":"1","temperatura":"20","Insertado_temp":"2016-08-16 12:30:29"}
    ]

I have obtained from the following code:

array_push($result,array(
//Pushing name and id in the blank array created
        "Id_temp"=>$row['Id_temp'],
        "temperatura"=>$row['temperatura'],
        "Insertado_temp"=>$row['Insertado_temp'],
        "Id_press"=>$row['Id_press'],
        "presion"=>$row['presion'],
        "Insertado_press"=>$row['Insertado_press']

    ));
$result1 = array_unique ($result);
}
echo json_encode(array('result'=>$result1));

mysqli_close($con);

I wish in PHP to delete the objects that have duplicate Id_temp and Id_press and to save it again in an array. I've tried it with "array_unique" and "array_values" but it did not work for me. Someone could give me an example based on this one that I describe. Thank you very much

    
asked by Oscar C. 08.09.2016 в 21:07
source

1 answer

1

In the following example I apply the following methodology:

Go through the array of values and save in 2 separate arrays $array_temp_fixed and $array_press_fixed the unique values interpreting that the values that should not be repeated are Id_temp and Id_press .

This is achieved by generating 2 arrays where I keep, separately, the values of the keys Id_temp and Id_press . Before saving these unique values, I verify that they do not exist in the corresponding array.

When I identify that a value does not exist in my unique array I add it so that the next time the cycle is executed, it does not enter this conditional, this is valid using the in_array function that searches for a certain value Id_temp / Id_press within the array specified. In the same step I keep in the final array $array_fixed the full row for later use.

Finally, just to facilitate understanding print the 4 arrays on the screen in the following order:

  • Original Array
  • Array of the unique values of the key Id_temp
  • Array of the unique values of the key Id_press
  • Final array including all rows without duplication
  •  <?php
    
    $json = '[ 
          {"Id_temp":"1","temperatura":"20","Insertado_temp":"2016-08-16 12:30:29"},
          {"Id_press":"1","presion":"34","Insertado_press":"2016-08-16 16:18:36"},
          {"Id_press":"1","presion":"34","Insertado_press":"2016-08-16 16:18:36"},
          {"Id_temp":"1","temperatura":"20","Insertado_temp":"2016-08-16 12:30:29"},
          {"Id_temp":"2","temperatura":"25","Insertado_temp":"2016-08-16 15:48:53"},
          {"Id_press":"2","presion":"34","Insertado_press":"2016-08-16 16:18:36"},
          {"Id_temp":"4","temperatura":"50","Insertado_temp":"2016-08-16 18:17:33"},
          {"Id_temp":"1","temperatura":"20","Insertado_temp":"2016-08-16 12:30:29"}
        ]';
    
    $array = json_decode($json, true);
    $array_temp_fixed = array();
    $array_press_fixed = array();
    $array_fixed = array();
    
    foreach($array as $key => $value){
        if (!empty($value["Id_temp"]) && !in_array($value["Id_temp"], $array_temp_fixed)){
            $array_temp_fixed[] = $value["Id_temp"];
            $array_fixed[] = $value;
        }
        if (!empty($value["Id_press"]) && !in_array($value["Id_press"], $array_press_fixed)){
            $array_press_fixed[] = $value["Id_press"];
            $array_fixed[] = $value;
        }
    }
    print_r($array);
    print_r($array_temp_fixed);
    print_r($array_press_fixed);
    print_r($array_fixed);
    ?>
    
        
    answered by 15.12.2016 / 16:12
    source