有什么方法可以检查 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_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
}
这似乎更有效率,尽管我想知道是否有更优雅的方式来解决这个问题。
预先感谢您。
答案 0 :(得分:1)
您可以扩展功能以跟踪更改本身。
if __name__ == '__main__':
app.run()
将function ianreadfile (csName,arrayno,callback){
var tArray = new Array();
Papa.parse(csName,{
delimiter: "",
newline: "",
download: true,
complete: function(results){
var data = results.data;
for(var i=0;i<data.length;i++){
var row = data[i];
tArray.push(row);
}
implement(arrayno);
}//end complete
});
callback();
}
function finished(){
alert("in finished");
var ian1="QQQQW";
}
ianreadfile (csName,arrayno,finished);
返回为2。
答案 1 :(得分:0)
使用array_diff比较两个数组。
if(array_diff($array_copy, $sample_array))
echo "not same";
答案 2 :(得分:0)
使用'count()'是恕我直言,这是最优雅的解决方案,我怀疑这可能会效率更高-您需要对其进行度量。 OTOH,供您考虑...
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;
}
答案 3 :(得分:0)
您可以使用闭包来更改布尔变量的状态:
$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";
结果:
Using closure: 9.1402053833008E-7 sec/run
Using array copy: 4.9885988235474E-7 sec/run
Using array count: 5.5881977081299E-7 sec/run
也许有助于决策:)
答案 4 :(得分:0)
如果要跟踪已删除的元素,可以执行以下操作。
$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);