note 75422 added to function.utf8-encode

From: Date: Tue, 29 May 2007 14:06:04 +0000
Subject: note 75422 added to function.utf8-encode
Groups: php.notes 
Request: Send a blank email to php-notes+get-126996@lists.php.net to get a copy of this message
If you are interested in recursively converting ISO-8859-1-encoded arrays into UTF-8, then this is one way to do it. Could use a small refactor though. (I used it to prepare some ISO-8859-1 arrays for json_encode. Note that for this to work your values and for associative arrays also your keys must be ISO-8859-1-encoded.) <?php /** * (Recursively) utf8_encode each value in an array. * * @param array $array * @return array utf8_encoded */ function utf8_encode_array($array) { if (is_array($array)) { $result_array = array(); foreach($array as $key => $value) { if (array_type($array) == "map") { // encode both key and value if (is_array($value)) { // recursion $result_array[utf8_encode($key)] = utf8_encode_array($value); } else { // no recursion if (is_string($value)) { $result_array[utf8_encode($key)] = utf8_encode($value); } else { // do not re-encode non-strings, just copy data $result_array[utf8_encode($key)] = $value; } } } else if (array_type($array) == "vector") { // encode value only if (is_array($value)) { // recursion $result_array[$key] = utf8_encode_array($value); } else { // no recursion if (is_string($value)) { $result_array[$key] = utf8_encode($value); } else { // do not re-encode non-strings, just copy data $result_array[$key] = $value; } } } } return $result_array; } return false; // argument is not an array, return false } /** * Determines array type ("vector" or "map"). Returns false if not an array at all. * (I hope a native function will be introduced in some future release of PHP, because * this check is inefficient and quite costly in worst case scenario.) * * @param array $array The array to analyze * @return string array type ("vector" or "map") or false if not an array */ function array_type($array) { if (is_array($array)) { $next = 0; $return_value = "vector"; // we have a vector until proved otherwise foreach ($array as $key => $value) { if ($key != $next) { $return_value = "map"; // we have a map break; } $next++; } return $return_value; } return false; // not array } ?> ---- Server IP: 212.47.220.13 Probable Submitter: 193.40.4.144 ---- Manual Page -- http://www.php.net/manual/en/function.utf8-encode.php Edit -- https://master.php.net/note/edit/75422 Del: integrated -- https://master.php.net/note/delete/75422/integrated Del: useless -- https://master.php.net/note/delete/75422/useless Del: bad code -- https://master.php.net/note/delete/75422/bad+code Del: spam -- https://master.php.net/note/delete/75422/spam Del: non-english -- https://master.php.net/note/delete/75422/non-english Del: in docs -- https://master.php.net/note/delete/75422/in+docs Del: other reasons-- https://master.php.net/note/delete/75422 Reject -- https://master.php.net/note/reject/75422 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#126996) next »