I would like to search something in array of objects and remove the array position. For example, follow the array:
$audits = [{"old_values":[],"new_values":[],"event":"updated","auditable_id":"440","auditable_type":"App\\Models\\User","user_id":"433","user_type":"App\\Models\\User","url":"http:\/\/protocolo-online2\/logout?","ip_address":"127.0.0.1","user_agent":"Mozilla\/5.0 (X11; Linux x86_64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/102.0.0.0 Safari\/537.36","tags":"","created_at":"2023-02-01 09:55:00","login":"mailson.suporte"},{"old_values":{"ultimo_login":"2023-02-01 09:32:16"},"new_values":{"ultimo_login":"2023-02-01 09:55:10"},"event":"updated","auditable_id":"440","auditable_type":"App\\Models\\User","user_id":"440","user_type":"App\\Models\\User","url":"http:\/\/protocolo-online2\/login?","ip_address":"127.0.0.1","user_agent":"Mozilla\/5.0 (X11; Linux x86_64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/102.0.0.0 Safari\/537.36","tags":"","created_at":"2023-02-01 09:55:10","login":"mailson.suporte"}]
I tried to do this code:
if (($k = array_search(440, array_column(json_decode($audits, true), 'user_id'))) !== false) {
unset($audits[$k]);
}
But this way is not working! I can do using foreach, this way:
foreach ($audits as $index => $audit) {
if($audit->user_id == 440){
unset($audits[$index]);
}
}
However if the array is start to grow up, I think that foreach is not a good method for this case.
Someone can help me?