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
Try this:
array_values
just sets normal numeric indexes in the result array.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:
You will get: