-3

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

user2110253
  • 317
  • 4
  • 12
  • 1
    http://php.net/manual/en/function.json-encode.php – fusion3k Apr 15 '16 at 12:51
  • 1
    did you have a try with json_encode ?? – Random Apr 15 '16 at 12:51
  • 1
    this is not json array format, this is string containing a byte-stream representation of value that can be stored anywhere. use `serialize($array)` to get this string format and `unserialize($serialized_data)` to get array format – Manjeet Barnala Apr 15 '16 at 13:01

3 Answers3

3

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";}
?>
Pupil
  • 23,834
  • 6
  • 44
  • 66
0

$json = json_encode($array);

and otherwise

$array = json_decode($json, true);

Reinder
  • 7
  • 3
0

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

user2110253
  • 317
  • 4
  • 12