Deepak Deepak - 7 days ago 6
PHP Question

how to serialize data in php?

I am using serialize() function.

Here is what I tried

$capability='Editor';
$id=1;
$three=array($capability.";".$id);
$abc=serialize($three);
print_r($abc);


Above code giving me output

a:1:{i:0;s:8:"Editor;1";}


and I want output in following form

a:1:{s:6:"editor";b:1;}


How to do this?

Thanks.

Answer

Let's first Unserialize your desired output. It says

Array
(
    [editor] => 1
)

So now we know, you are not building the array correctly. Do

$three=array($capability=>$id);  

Fiddle

To produce a Boolean value you can use TRUE instead of 1.

 $id=TRUE;  

And the result will be

a:1:{s:6:"Editor";b:1;}

Fiddle