lopes_andre Posted October 12, 2010 Share Posted October 12, 2010 Hi, I'am trying to convert to UTF8 an array using serialize() but it is not working. The array: Array ( [0] => Array ( [zona] => _Todas [conta] => 26 ) [1] => Array ( [zona] => Braga [conta] => 1 ) [2] => Array ( [zona] => Coimbra [conta] => 1 ) [3] => Array ( [zona] => Leiria [conta] => 1 ) [4] => Array ( [zona] => Lisboa [conta] => 8 ) [5] => Array ( [zona] => Porto [conta] => 13 ) [6] => Array ( [zona] => Santarém [conta] => 1 ) [7] => Array ( [zona] => Viseu [conta] => 1 ) ) Note that "[6] => Array ( [zona] => Santarém [conta] => 1 )" have some strange characters so I have used serialize function to convert this array to string, and then I get: a:8:{i:0;a:2:{s:4:"zona";s:6:"_Todas";s:5:"conta";s:2:"26";}i:1;a:2:{s:4:"zona";s:5:"Braga";s:5:"conta";s:1:"1";}i:2;a:2:{s:4:"zona";s:7:"Coimbra";s:5:"conta";s:1:"1";}i:3;a:2:{s:4:"zona";s:6:"Leiria";s:5:"conta";s:1:"1";}i:4;a:2:{s:4:"zona";s:6:"Lisboa";s:5:"conta";s:1:"8";}i:5;a:2:{s:4:"zona";s:5:"Porto";s:5:"conta";s:2:"13";}i:6;a:2:{s:4:"zona";s:9:"Santarém";s:5:"conta";s:1:"1";}i:7;a:2:{s:4:"zona";s:5:"Viseu";s:5:"conta";s:1:"1";}} Then I have used this string and have applicated utf8_decode(), and I get the correct characters for my language: a:8:{i:0;a:2:{s:4:"zona";s:6:"_Todas";s:5:"conta";s:2:"26";}i:1;a:2:{s:4:"zona";s:5:"Braga";s:5:"conta";s:1:"1";}i:2;a:2:{s:4:"zona";s:7:"Coimbra";s:5:"conta";s:1:"1";}i:3;a:2:{s:4:"zona";s:6:"Leiria";s:5:"conta";s:1:"1";}i:4;a:2:{s:4:"zona";s:6:"Lisboa";s:5:"conta";s:1:"8";}i:5;a:2:{s:4:"zona";s:5:"Porto";s:5:"conta";s:2:"13";}i:6;a:2:{s:4:"zona";s:9:"Santarém";s:5:"conta";s:1:"1";}i:7;a:2:{s:4:"zona";s:5:"Viseu";s:5:"conta";s:1:"1";}} But then when I try to use the unserialize() function to convert to array again I get this error: A PHP Error was encountered Severity: Notice Message: unserialize() [function.unserialize]: Error at offset 364 of 440 bytes Filename: controllers/v1.php Line Number: 23 I know this error is caused by the character with accent decoded to UTF8. My question, how to solve this problem? I need to have the correct the bad character in the array that comes from the database. There is other options than use serialize() and unserialize()? PS: The array is for building an JSON so I'am not passing nothing to define the charset. It is possible to send in the headers the charset? PS2: Sorry for my bad english. Best Regards, Link to comment https://forums.phpfreaks.com/topic/215717-it-is-possible-to-use-serializeunserialize-to-do-this/ Share on other sites More sharing options...
lopes_andre Posted October 13, 2010 Author Share Posted October 13, 2010 Sorry for reply to my own post. Can someone give me a clue on this subject? Best Regards, Link to comment https://forums.phpfreaks.com/topic/215717-it-is-possible-to-use-serializeunserialize-to-do-this/#findComment-1121968 Share on other sites More sharing options...
Zane Posted October 13, 2010 Share Posted October 13, 2010 you might want to utf8_decode BEFORE serializing foreach($array as $value) utf8_decode($value); $s_array = serialize($array); echo "", print_r($s_array), ""; Link to comment https://forums.phpfreaks.com/topic/215717-it-is-possible-to-use-serializeunserialize-to-do-this/#findComment-1121982 Share on other sites More sharing options...
lopes_andre Posted October 16, 2010 Author Share Posted October 16, 2010 Hi, thanks for the reply. Unfortunately the solution does not fit. I have this kind of array: Array ( [0] => Array ( [zona] => _Todas [conta] => 26 ) [1] => Array ( [zona] => Braga [conta] => 1 ) [2] => Array ( [zona] => Coimbra [conta] => 1 ) [3] => Array ( [zona] => Leiria [conta] => 1 ) [4] => Array ( [zona] => Lisboa [conta] => 8 ) [5] => Array ( [zona] => Porto [conta] => 13 ) [6] => Array ( [zona] => Santarém [conta] => 1 ) [7] => Array ( [zona] => Viseu [conta] => 1 ) ) How can I do that with this type of array? Best Regards, Link to comment https://forums.phpfreaks.com/topic/215717-it-is-possible-to-use-serializeunserialize-to-do-this/#findComment-1122721 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.