Ankit Shah Ankit Shah - 1 month ago 11
PHP Question

How to get Serialize Output Data?

I need Output in this format:

a:1:{s:11:"category_id";s:2:"1";}


I tried below code

$a = array('category_id',1);

echo serialize($a);


Result:
a:2:{i:0;s:11:"category_id";i:1;i:41;}


How to get above result? I also tried
json_encode

Answer

Rewrite the array as

$a = array('category_id'=> "1 ");

As the serialised params are in the format type:length:value, you need to pass 1 as a string.

Also s:2:"1" won't be possible as 1 has a length of 1 not 2. If you wan't a string of 2 length, maybe we can pass <space> to acheive that.