note 75435 added to function.utf8-encode
| From: | hillar dot petersen at gmail dot com | Date: | Wed, 30 May 2007 05:59:05 +0000 |
| Subject: | note 75435 added to function.utf8-encode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-127009@lists.php.net to get a copy of this message | ||
In addition to my previous post. If your values are already in utf-8 maybe you want to utf8_encode
array keys only. This will do it:
<?php
/**
* (Recursively) utf8_encode all array keys.
*
* @param array $array
* @return array with utf8_encoded keys
*/
function utf8_encode_array_keys($array)
{
$array_type = array_type($array);
if ($array_type == "map")
{
$result_array = array();
foreach($array as $key => $value)
{
if (is_array($value))
{
// recursion
$result_array[utf8_encode($key)] = utf8_encode_array_keys($value);
}
else
{
// value is not an array, no recursion
$result_array[utf8_encode($key)] = $value;
}
}
return $result_array;
}
else if ($array_type == "vector")
{
// do not encode anything, just follow the value if it is an array
$result_array = array();
foreach ($array as $key => $value)
{
if (is_array($value))
{
// recursion
$result_array[$key] = utf8_encode_array_keys($value);
}
else
{
// value is not an array, no recursion
$result_array[$key] = $value;
}
}
return $result_array;
}
return false; // argument is not an array, return false
}
?>
Also note that both this operation (with keys only) and the operation with both keys and values can
be reversed by replacing "encode" by "decode".
----
Server IP: 212.47.220.13
Probable Submitter: 80.235.60.114
----
Manual Page -- http://www.php.net/manual/en/function.utf8-encode.php
Edit -- https://master.php.net/note/edit/75435
Del: integrated -- https://master.php.net/note/delete/75435/integrated
Del: useless -- https://master.php.net/note/delete/75435/useless
Del: bad code -- https://master.php.net/note/delete/75435/bad+code
Del: spam -- https://master.php.net/note/delete/75435/spam
Del: non-english -- https://master.php.net/note/delete/75435/non-english
Del: in docs -- https://master.php.net/note/delete/75435/in+docs
Del: other reasons-- https://master.php.net/note/delete/75435
Reject -- https://master.php.net/note/reject/75435
Search -- https://master.php.net/manage/user-notes.php