Jump to content

It is possible to use serialize/unserialize to do this?


lopes_andre

Recommended Posts

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,

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,

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.