php - Makes preg_grep keep and not reorder the arrays keys after each execution -
i'm trying clean email database imported excel table. there's plenty of bad characters, , 2 or 3 emails in same cell. dont want use direct database solution posted here (t-sql: checking email format), because double check eye before excluding it.
1) first got of emails weren't shaped. transformed them on array. note i'm in laravel ecosystem.
$contato = db::select("select * emailstable outro_email not regexp '^[a-za-z0-9][a-za-z0-9._-]*[a-za-z0-9]@[a-za-z0-9][a-za-z0-9._-]*[a-za-z0-9]\.[a-za-z]{2,4}$'"); $email_array = json_decode(json_encode($email_database_as_object), true);
2) those, eliminated records not have @ symbol on (empty, null, random phrases), excluding them original array:
$corretor = preg_grep("/@/i", array_column($email_array, "email"), preg_grep_invert); foreach ($corretor $key => $value) { $email_array = array_except($email_array, array($key)); }
but biggest problem when i'm trying remove bad characters, preg_grep assigns resulting array, new array keys. instead of keeping original ones.
as exemple: original array keys filtered: 1,4,10,24,34,65,78 (7 keys) assigned keys: 0,1,2,3,4,5,6
on code, i'm trying extract multiple emails inserted in 1 cell, through separators ";" , ",' , " ", using preg_grep:
$email_corrected = array(); //array of corrected emails $corretor = preg_grep("/;/i", array_column($email_array, "outro_email")); foreach ($corretor $key => $value) { $provisorio = explode(';', $value); $provisorio = array_where($provisorio, function($chave, $valor) { return strlen($valor) > 0; }); //laravel function take out empty results of explode $provisorio=array_map('trim',$provisorio); $email_corrected[$key] = $provisorio; //adds result corrected emails $email_array = array_except($email_array, array($key)); // takes out result original array } $corretor = preg_grep("/,/i", array_column($email_array, "outro_email")); foreach ($corretor $key => $value) { $provisorio = explode(',', $value); $provisorio = array_where($provisorio, function($chave, $valor) { return strlen($valor) > 0; }); $provisorio=array_map('trim',$provisorio); $email_corrected[$key] = $provisorio; $email_array = array_except($email_array, array($key)); } $corretor = preg_grep("/\//", array_column($contato_array, "outro_email")); foreach ($corretor $key => $value) { $provisorio = explode(',', $value); $provisorio = array_where($provisorio, function($chave, $valor) { return strlen($valor) > 0; }); $provisorio=array_map('trim',$provisorio); $email_corrected[$key] = $provisorio; $email_array = array_except($email_array, array($key)); }
but after each preg_grep, keys $corretor not match original keys $email_array. result, im not eliminating correct keys original array when doing $email_array = array_except($email_array, array($key));
thanks in advance.
Comments
Post a Comment