php - Decode a string from an array within an array back into an array

561

I have the following array (for instance)

Array
(
    [0] => 2
    [1] => 134
    [2] => 7
)

Inside another array, of the form

Array
(
    [_page_list] => Array
        (
            [0] => a:3:{i:0;s:1:"2";i:1;s:3:"134";i:2;s:1:"7";}
        )
    [some more data...]
)

This has been put into a Json and then decoded back again, giving me as an element just the string "a :3:{i:0;s:1:\"2\";i:1;s:3:\"134\";i:2;s:1:\"7\";}" inside the bigger array. This doesn't seem to be itself a json string, nor can it be simply cast back into an array.

How can I cast this string back into the proper array it used to be?

198

Answer

Solution:

That's a PHP serialized string, see the documentation here:

http://php.net/manual/en/function.serialize.php

You can decode it withunserialize, e.g.

unserialize('a:3:{i:0;s:1:"2";i:1;s:3:"134";i:2;s:1:"7";}')

=

Array
(
    [0] => 2
    [1] => 134
    [2] => 7
)

People are also looking for solutions to the problem: macos - PHP: Unable to load dynamic library intl.so (OSX)

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.