Voting

: eight minus six?
(Example: nine)

The Note You're Voting On

espertalhao04 at hotmail dot com
12 years ago
here is a simple and yet easy to use implementation of this function.
the 'original' function has the problem that you can't unset a value.
with my function, YOU CAN!

<?php
function array_walk_protected(&$a,$s,$p=null)
{
if(!
function_exists($s)||!is_array($a))
{
return
false;
}

foreach(
$a as $k=>$v)
{
if(
call_user_func_array($s,array(&$a[$k],$k,$p))===false)
{
unset(
$a[$k]);
}
}
}

function
get_name(&$e,$i,$p)
{
echo
"$i: $e<br>";
return
false;
}

$m=array('d'=>'33','Y'=>55);

array_walk_protected($m,'get_name');

var_dump($m); //returns array(0) { }
?>

i called it array_walk_protected because it is protected against the unexpected behavior of unsetting the value with the original function.

to delete an element, simply return false!!!
nothing else is needed!
unsetting $e, under your created function, will keep the same array as-is, with no changes!

by the way, the function returns false if $a is not array or $s is not a string!

limitations: it only can run user defined functions.
i hope you like it!

<< Back to user notes page

To Top