skip to Main Content

I have this object in php:

$object = 
[
    [
        {"catalogo": "C400047", "rfc_inf_aval": "CIS981002NK4", },
        {"catalogo": "C140064","rfc_inf_aval": "MZT8501014S6",},
        {"catalogo": "C400047","rfc_inf_aval": "MZT8501014S6",},
        {"catalogo": "C400047","rfc_inf_aval": "CIS981002NK4",},
        {"catalogo": "C140064","rfc_inf_aval": "MZT8501014S6",},
        {"catalogo": "C140064","rfc_inf_aval": "MZT8501014S6",},
        {"catalogo": "C140064","rfc_inf_aval": "MZT8501014S6",},
        {"catalogo": "C140064","rfc_inf_aval": "CIS981002NK4",},   
    ],
]

and it should stay like this, that I eliminate all the repeated rfc of each catalog, the repeated catalogs should not be eliminated

[
    [
        {"catalogo": "C400047","rfc_inf_aval": "CIS981002NK4",},
        {"catalogo": "C140064","rfc_inf_aval": "MZT8501014S6",},
        {"catalogo": "C400047","rfc_inf_aval": "MZT8501014S6",},
        {"catalogo": "C140064","rfc_inf_aval": "CIS981002NK4",},
    ],
]

I have tried to do this but it removes all the rfcs and I need it to remove only the repeated rfcs but by catalog

  for ($i=0; $i < count($object); $i++) { 
                 if(!in_array($object[$i]->rfc_inf_aval, $array1)){
                     array_push($array1,  $object[$i]->rfc_inf_aval);
                      array_push($array2,  $object[$i]);
                 }
             }   

2

Answers


  1. Try this:

     for ($i=0; $i < count($object); $i++) { 
         $k = $object[$i]->catalogo.'|'.$object[$i]->rfc_inf_aval;
         $reduced[$k] = $object[$i];
     }
     $object = array_values($reduced);
    
    • I generate a key from catalogo and rfc_inf_aval. That eliminates the duplication in the new array.
    • array_values just sets normal numeric indexes in the result array.
    Login or Signup to reply.
  2. First, your object is invalid, since is a JSON string, so I use heredoc to define it.

    Following this answer https://stackoverflow.com/a/25020035/1757214 I get the following code:

    <?php 
    
    $js = <<<JSON
    [[
            {"catalogo": "C400047","rfc_inf_aval": "CIS981002NK4"},
            {"catalogo": "C140064","rfc_inf_aval": "MZT8501014S6"},
            {"catalogo": "C400047","rfc_inf_aval": "MZT8501014S6"},
            {"catalogo": "C400047","rfc_inf_aval": "CIS981002NK4"},
            {"catalogo": "C140064","rfc_inf_aval": "MZT8501014S6"},
            {"catalogo": "C140064","rfc_inf_aval": "MZT8501014S6"},
            {"catalogo": "C140064","rfc_inf_aval": "MZT8501014S6"},
            {"catalogo": "C140064","rfc_inf_aval": "CIS981002NK4"} 
    ]]
    JSON;
    // Get the real object as an associative array
    $data = json_decode($js, true);
    // Use the first element, since is a nested array
    $object = $data[0];
    
    function my_array_unique($array, $keep_key_assoc = false){
        $duplicate_keys = array();
        $tmp = array();       
    
        foreach ($array as $key => $val){
            // convert objects to arrays, in_array() does not support objects
            if (is_object($val))
                $val = (array)$val;
    
            if (!in_array($val, $tmp))
                $tmp[] = $val;
            else
                $duplicate_keys[] = $key;
        }
    
        foreach ($duplicate_keys as $key)
            unset($array[$key]);
    
        return $keep_key_assoc ? $array : array_values($array);
    }
    
    var_dump(my_array_unique($object));
    

    You will get:

    array(4) {
      [0]=>
      array(2) {
        ["catalogo"]=>
        string(7) "C400047"
        ["rfc_inf_aval"]=>
        string(12) "CIS981002NK4"
      }
      [1]=>
      array(2) {
        ["catalogo"]=>
        string(7) "C140064"
        ["rfc_inf_aval"]=>
        string(12) "MZT8501014S6"
      }
      [2]=>
      array(2) {
        ["catalogo"]=>
        string(7) "C400047"
        ["rfc_inf_aval"]=>
        string(12) "MZT8501014S6"
      }
      [3]=>
      array(2) {
        ["catalogo"]=>
        string(7) "C140064"
        ["rfc_inf_aval"]=>
        string(12) "CIS981002NK4"
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search