You are here: array_map in conjunction with user_call_func_array - inconsistency « PHP « IT news, forums, messages
array_map in conjunction with user_call_func_array - inconsistency

Posted by Jochem Maas on 10/04/90 11:07

listers,

I was playing around with call_user_func_array() and array_map() on
PHP 5.0.2 (cli) (built: Nov 9 2004 19:00:36) and noticed the that
calling call_user_func_array() on 'array_map' with more than 2 args
(i.e. more than just the name of the callback and array
argument that are required for 'array_map') then a numerically indexed array
is returned, where as with just the minimum 2 args the associative keys of array
are maintained:

question is have I missed something, am I doing something wrong or it this
a 'buglet'?


===========================================================================

function array_map_assoc($callback, $arr)
{
$keys = array_keys($arr);
$args = func_get_args();

$arr = call_user_func_array("array_map", $args);

/* return the fixed array */
return $arr;
}

// couldn't figure out how else
// to use a single quote with php -r in a bash shell
$sq = chr(39);

// start array
$arr1 = array( "name" => "\"{$sq}Testing{$sq}\"",
"email" => "\"Yadda{$sq}s \"YADDa\"<script language={$sq}Testing{$sq} source={$sq}{$sq}></script>\"");

$arr2 = array_map_assoc("strip_tags", $arr1);
$arr3 = array_map_assoc("htmlentities", $arr2, array( ENT_QUOTES, ENT_QUOTES ));
$arr4 = array_map_assoc("htmlentities", $arr2);
$arr5 = array_map_assoc("strip_tags", $arr1, array( "<p>", "<p>" ));


echo "-----\n\n";
var_dump($arr1, $arr2, $arr3, $arr4, $arr5);


===========================================================================


OUTPUTS ON MACHINE:
-----
array(2) {
["name"]=>
string(11) ""'Testing'""
["email"]=>
string(63) ""Yadda's "YADDa"<script language='Testing' source=''></script>""
}
array(2) {
["name"]=>
string(11) ""'Testing'""
["email"]=>
string(17) ""Yadda's "YADDa"""
}
array(2) {
[0]=>
string(31) "&quot;&#039;Testing&#039;&quot;"
[1]=>
string(42) "&quot;Yadda&#039;s &quot;YADDa&quot;&quot;"
}
array(2) {
["name"]=>
string(21) "&quot;'Testing'&quot;"
["email"]=>
string(37) "&quot;Yadda's &quot;YADDa&quot;&quot;"
}
array(2) {
[0]=>
string(11) ""'Testing'""
[1]=>
string(17) ""Yadda's "YADDa"""
}



rgds,
Jochem

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация