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.

1

1 Answer 1

5

Try by using:

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

To revert back:

$input = unserialize(gzuncompress(base64_decode($data)));
Sign up to request clarification or add additional context in comments.

2 Comments

welcome to SO. I found the solution also. Appreciate your interest and enthusiasm to post answer on old questions. :-)
How can unserialize from client side? Say, Javascript or dart?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.