0

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]);
                 }
             }   
  • Ok, so show us your best attempt – RiggsFolly Nov 11 '22 at 17:06
  • PS you $object is in fact an array of objects – RiggsFolly Nov 11 '22 at 17:06
  • What is your original data? JSON? – Foobar Nov 11 '22 at 17:10
  • this is original 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",}, ], – christian alvarez Nov 11 '22 at 17:14
  • Well is nearly JSON :) Its fixable by removing the lazy commas – RiggsFolly Nov 11 '22 at 17:14
  • @RiggsFolly Yeah, but not me :-) – Foobar Nov 11 '22 at 17:15
  • _this is original json_ Then its invalid JSON, try putting it into `https://jsonlint.com` – RiggsFolly Nov 11 '22 at 17:16
  • I need with an algorithm to make the first array of objects be like the second from some algorithm. the object is created from a database query the query is this SELECT catalog,rfc_inf_guarantee from V_ANEXO_18_IFRS9_GARANTE – christian alvarez Nov 11 '22 at 17:17
  • I know what you need, but uinless the data is valid JSON then it cannot be done, well not by me as I am not going to try and remove allthe LAZY trailing commas. PHP allows them now but JSON does not – RiggsFolly Nov 11 '22 at 17:19
  • _the object is created from a database query_ Then you need to look at whatever is storing this data, either it is turning JSON into nonsense or you are building JSON manually somewhere and its not being done right – RiggsFolly Nov 11 '22 at 17:21
  • checkout `json_encode()` and `json_decode()` If you are building JSON an other way, you run the risk of creating invalid json – RiggsFolly Nov 11 '22 at 17:23
  • Does this answer your question? [PHP Removing duplicate objects from array](https://stackoverflow.com/questions/24558484/php-removing-duplicate-objects-from-array) – manuerumx Nov 11 '22 at 17:40

2 Answers2

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.
Foobar
  • 769
  • 1
  • 4
  • If the OP can find a way to get valid JSON and convert it to a PHP Array of objects, this works very nicely :) Would have used a foreach myself, but thats just semantics – RiggsFolly Nov 11 '22 at 17:27
  • @RiggsFolly Oh, i just updated his last code snippet, with the `for` :-) And only was taking the code snippet and "I need it to remove only the repeated rfcs but by catalog" into account for my answer. – Foobar Nov 11 '22 at 17:34
  • @christianalvarez nice :-) – Foobar Nov 11 '22 at 17:36
1

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"
  }
}
manuerumx
  • 1,230
  • 14
  • 28