<?php
$array = [
0 => [
"id" => 1,
"name" => "Sammy",
"phone" => "2348055643322",
"email" => "sammy@abc.com"
],
1 => [
"id" => 2,
"name" => "Saheed",
"phone" => "",
"email" => "saay@aol.com"
],
2 => [
"id" => 3,
"name" => "Edwin",
"phone" => "2348099993322",
"email" => "eddy@gmail.com"
],
3 => [
"id" => 3,
"name" => "Jonadab",
"phone" => "2348066773322",
"email" => "jonadab@gmail.com"
],
4 => [
"id" => 3,
"name" => "Mercy",
"phone" => "",
"email" => "mercy@sysnet.com"
]
];
$start = microtime(true);
$phone = array_filter(array_column($array, "phone"));
$filtered = array_intersect_key($array, $phone);
Echo "array_intersect_key: " . (microtime(true)-$start)*1000;
$start = microtime(true);
$column = 'phone'; //column to filter
$result = array_filter($array, function($o) use($column) {
return trim( $o[$column] ) !== '' && $o[$column] !== null;
});
Echo "\n\narray_filter: " . (microtime(true)-$start)*1000;
$start = microtime(true);
foreach ($array as $index => $row) {
if (strlen($row['phone'])) {
// keep the original index
$response [$index]= $row;
}
}
Echo "\n\nforeach: " . (microtime(true)-$start)*1000;
$start = microtime(true);
$reulting_array = array_filter($array, function ($entry) {
return !empty($entry['phone']);
});
Echo "\n\narray_filter by SaAy: " . (microtime(true)-$start)*1000;
preferences:
24.03 ms | 408 KiB | 5 Q