note 75435 deleted from function.utf8-encode by cmb
| From: | cmb@php.net | Date: | Sun, 04 Dec 2016 12:11:24 +0000 |
| Subject: | note 75435 deleted from function.utf8-encode by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208193@lists.php.net to get a copy of this message | ||
Note Submitter: hillar dot petersen at gmail dot com
----
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".