PHP:检查array_filter是否从数组中删除了任何条目
有没有办法检查array_filter是否从数组中删除了任何值 我想到的解决方案是:PHP:检查array_filter是否从数组中删除了任何条目,php,array-filter,Php,Array Filter,有没有办法检查array_filter是否从数组中删除了任何值 我想到的解决方案是: $sample_array = ["values"]; $array_copy = $sample_array; if (array_filter($sample_array, 'fn') === $array_copy) { #Some value was deleted from the array } else { #The array was not modified } 这个似乎不
$sample_array = ["values"];
$array_copy = $sample_array;
if (array_filter($sample_array, 'fn') === $array_copy) {
#Some value was deleted from the array
} else {
#The array was not modified
}
这个似乎不是很有效,因为它必须复制整个阵列
$sample_array = ["values"];
$array_count = count($sample_array);
if (count(array_filter($sample_array, 'fn')) === $array_count) {
#The array was not modified
} else {
#Some value was deleted from the array
}
这一条似乎更有效,尽管我想知道是否有更优雅的方式来处理这个问题
事先谢谢。用于比较两个阵列
if(array_diff($array_copy, $sample_array))
echo "not same";
使用“count()”是最优雅的解决方案,我怀疑,它可能更有效——您需要对其进行度量。奥托,为了你的考虑
function fn($arg, $mode='default')
{
static $matched;
if ('default'==$mode) {
$result=real_filter_fn($arg);
$matched+=$result ? 1 : 0;
return $result;
}
$result=$matched;
$matched=0;
return $result;
}
您可以扩展您的功能以跟踪更改本身
$array = [1, 2, 3, 4];
$changed = 0;
$new = array_filter($array, function($e) use (&$changed) {
$result = $e <= 2;
if(!$result) $changed++;
return $result;
});
$array=[1,2,3,4];
$changed=0;
$new=array\u filter($array,function($e)use(&$changed){
$result=$e您可以使用闭包来更改布尔变量的状态:
$sample_array = [1,2,3,4];
$array_changed = false;
//Use closure to change $array_changed
print_r(array_filter($sample_array, function($var) use (&$array_changed) {
$check = (!($var & 1));
$array_changed = $array_changed?:$check;
return $check;
}));
if ($array_changed) {
#Some value was deleted from the array
} else {
#The array was not modified
}
[编辑]
与我的解决方案相比,这是一个简单的速度测试:
/**********************SPEEDTEST**************************/
function fn ($var) {
return (!($var & 1));
}
$sample_array = [1,2,3,4];
$before = microtime(true);
for ($i=0 ; $i<100000 ; $i++) {
$array_changed = false;
//Use closure to change $array_changed
array_filter($sample_array, function($var) use (&$array_changed) {
$check = (!($var & 1));
$array_changed = $array_changed?:$check;
return $check;
});
if ($array_changed) {
#Some value was deleted from the array
} else {
#The array was not modified
}
}
$after = microtime(true);
echo "Using closure: " . ($after-$before)/$i . " sec/run\n";
echo '<br>';
/*******************************************************/
$before = microtime(true);
for ($i=0 ; $i<100000 ; $i++) {
$array_copy = $sample_array;
if (array_filter($sample_array, 'fn') === $array_copy) {
#Some value was deleted from the array
} else {
#The array was not modified
}
}
$after = microtime(true);
echo "Using array copy: " . ($after-$before)/$i . " sec/run\n";
echo '<br>';
/*******************************************************/
$before = microtime(true);
for ($i=0 ; $i<100000 ; $i++) {
$array_count = count($sample_array);
if (count(array_filter($sample_array, 'fn')) === $array_count) {
#The array was not modified
} else {
#Some value was deleted from the array
}
}
$after = microtime(true);
echo "Using array count: " . ($after-$before)/$i . " sec/run\n";
也许这有助于决策:)如果您想跟踪删除的元素,可以这样做
$sample_array = ['values', 'test'];
$removed = []; // Contains all the removed values.
$new_array = array_filter($sample_array, function($value) use (&$removed) {
if($value == 'test') {
$removed[] = $value; // Adds the value to the removed array.
return false;
}
return true;
});
print_r($removed);
我看你的第二个解决方案没有任何问题。只要你不使用多维数组,这应该没问题。正是我所想的。你仍然需要迭代数组的处理成本,以及更大的内存使用率。但是你仍然需要复制数组,这是否有效?问题不是效率cy.这是关于比较的。这是一种使用本机函数的简单方法。谁告诉你必须使用它的?总是有很多方法可以得到相同的结果。因为肯定没有数百万个元素,所以它不会遇到问题。我还提供了一个“高效的”我认为这里的评论指向了错误的方向。也许你的函数的解释和使用示例会对OP有所帮助。如果OP不能理解我的代码,那么OP就没有依据来评估它是否满足他/她的要求。不,这不起作用,因为过滤完成后,这个标志只会表示最后一个数组项的状态-如果该项保留在数组中,则无论以前删除了多少项,该项都将为true。如果删除了项,则必须将$array_changed设置为true,但对于保留的项,则不得将其设置为false。
$sample_array = ['values', 'test'];
$removed = []; // Contains all the removed values.
$new_array = array_filter($sample_array, function($value) use (&$removed) {
if($value == 'test') {
$removed[] = $value; // Adds the value to the removed array.
return false;
}
return true;
});
print_r($removed);