如何在php中将数组转换为JSON对象[重复]

This question already has an answer here:

I want to convert my array value:

Array ( [page_1] => fifth [page_2] => first [page_3] => fourth [page_4] => third ) 

Into JSON format is given below

{s:6:"page_1";s:5:"third";s:6:"page_2";s:5:"first";s:6:"page_3";s:6:"fourth";s:6:"page_4";s:5:"fifth";}

Can anyone please help me

</div>

You want to serialize you array.

You need to use serialize()

<?php
$a = array (
        'page_1' => 'fifth',
        'page_2' => 'first',
        'page_3' => 'fourth',
        'page_4' => 'third');
echo serialize($a);
// Outputs: a:4:{s:6:"page_1";s:5:"fifth";s:6:"page_2";s:5:"first";s:6:"page_3";s:6:"fourth";s:6:"page_4";s:5:"third";}
?>

$json = json_encode($array);

and otherwise

$array = json_decode($json, true);

When I insert the value in table it is inserting like

s:107:"a:4:{s:6:"page_1";s:5:"third";s:6:"page_2";s:5:"first";s:6:"page_3";s:6:"fourth";s:6:"page_4";s:5:"fifth";}";

don't know why cause when i display it that is right but in table it is inserting something like above