any solution about array_walk with pass-by-reference UseData?

any solution about array_walk with pass-by-reference UseData?

am 12.01.2010 20:39:52 von hack988 hack988

In Online Document say's:
Users may not change the array itself from the callback function. e.g.
Add/delete elements, unset elements, etc. If the array that
array_walk() is applied to is changed, the behavior of this function
is undefined, and unpredictable.

So I'm use Use Optional param( [, mixed $userdata ])

here is my code
========================================================
$return=array();
$disable_full=array('a','b','c');
$disable_start=array('_','!','HTTP'/*,'ddd','ddd','ddd','ddd ','ddd'*/);
$check_array=array("a"=>1,"_POST"=>'c',"HTTP"=>"f","ddd"=>ar ray('fefe'));
array_walk($check_array,'walk_fun_with_foreach',&$return);

print_r($return);

function walk_fun_with_foreach(&$source,$key,$return){
global $disable_full,$disable_start;
//var_dump($key);
if(is_array($source)){
array_walk($source,'walk_fun_with_foreach',&$return);
}else{

if(in_array(strval($key),$disable_full)){
//exit;
return;
}else{
foreach($disable_start as $key1 => $value){
if(strpos($key,$value)===0){
//echo $key;
return;
}
}
}
//echo $key;
$return[$key]=$source;
}
}
==========================================================
it's print :
Array ( [0] => fefe )

but in version php5.2.x and higher version,display Warning message:
Warning: Call-time pass-by-reference has been deprecated in

If I'm change
array_walk($check_array,'walk_fun_with_foreach',&$return);
to
array_walk($check_array,'walk_fun_with_foreach',$return);

and
declare function like
function walk_fun_with_foreach(&$source,$key,&$return)
no Warning Display
But result is not my needed.
Anyone has solution about is?I'm don't like use foreach for
instead,beacuse of some preformance reason.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: [PHP-DEV] any solution about array_walk with pass-by-reference

am 13.01.2010 12:33:25 von hack988 hack988

Online document say's
Note: Please note that this function only checks one dimension of a
n-dimensional array. Of course you can check deeper dimensions by
using, for example, array_udiff_uassoc($array1[0], $array2[0],
"data_compare_func", "key_compare_func");.

It's not my needed

My need is
$disable_full=3Darray("a","b","c");
$disable_start=3Darray("_","HTTP");

$check_array=3Darray(array("http"=3D>"a"),"a"=3D>"dabdd");


I want to unset all element's key full match in $disable_full and
start with string in $disable_start array;

2010/1/13 Joey Smith :
> This might be better served by taking it to php-general, because I don't
> think you need to pin your question so hard to the behaviour of
> array_walk(). Here's a quick example of (if I understood your question
> correctly) how you might solve it using array_udiff_uassoc and 5.3's new
> 'closure' syntax (not required for this, but I do so enjoy using them!)
>
> > $return=3Darray();
> $disable_full=3Darray('a','b','c');
> $disable_start=3Darray('_','!','HTTP'/*,'ddd','ddd','ddd','d dd','ddd'*/);
> $check_array=3Darray("a"=3D>1,"_POST"=3D>'c',"HTTP"=3D>"f"," ddd"=3D>array=
('fefe'));
>
> function buildFilter($keys, $starts_with) {
> =A0 return function ($a, $b) use ($keys, $starts_with) {
> =A0 =A0 =A0if (in_array($a, $keys)) return 0;
> =A0 =A0 =A0foreach($starts_with as $value) if (strpos($a, $value)===
=3D0) return 0;
> =A0 =A0 =A0return 1;
> =A0 };
> }
>
> $foo =3D buildFilter($disable_full, $disable_start);
>
> var_dump(array_udiff_uassoc($check_array, $disable_full, $disable_start, =
function () { return 0; }, $foo));
>
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php