2

I am compressing the array as below,

<?php
    $input = array('name'=>'PHP');
    $compressed = gzcompress(serialize($input));

    echo '<pre>'; print_r($compressed);
?>

And it is printing the data as below

xœK´2´ª.¶2±RÊKÌMU².¶2¶R
ðP²®nJ»

Now, I would like to convert $compressed as a json string. Since the compressed data contains special characters, it throws error, So I tried JSON_UNESCAPED_UNICODE below is the code snippet.

<?php
    // $compressed value getting from above script
    echo json_encode($compressed,JSON_UNESCAPED_UNICODE);
?>

Still it is not working. Can anyone suggest how to encode a compressed data as a json string.

Anto S
  • 2,448
  • 6
  • 32
  • 50
  • 1
    You might want to look into this earlier post on the subject: http://stackoverflow.com/questions/1443158/binary-data-in-json-string-something-better-than-base64 – Carsten Massmann Aug 17 '15 at 05:28

1 Answers1

4

Try by using:

$data = base64_encode(gzcompress(serialize($input)));

To revert back:

$input = unserialize(gzuncompress(base64_decode($data)));
Shalev Shalit
  • 1,945
  • 4
  • 24
  • 34
Saro
  • 64
  • 2