[ create a new paste ] login | about

Link: http://codepad.org/ioq5rgB2    [ raw code | output | fork ]

PHP, pasted on May 5:
<?php
$config = array(
    "type"  => '{"category":"admin","page":"page"}',
    "say"     => "Hello",
    "php"   => array(
        "say"     => "no",
        "type"  => '{"category":"admin","page":"page"}',
        "gran"  =>array(
            "name" => "Hi"
        )
    )
);
class objectify
{

    public function json_to_array($array, $recursive = true)
    {
        # if $array is not an array, let's make it array with one value of former $array.
        if (!is_array($array)) $array = array($array);

        foreach($array as $key => $value)
        {
            if ($recursive === false)
            {
               $array[$key] = (!empty($value) && is_string($value) && json_decode($value) != NULL) ? json_decode($value, true): $value;
            }
            else
            {
               $array[$key] = (!empty($value) && is_string($value) && json_decode($value) != NULL)
                                   ? json_decode($value, true)
                                   : (is_array($value) ? self::json_to_array($value) : $value);
            }
        }


        return $array;
    }
}
$object = new objectify();
$config = $object->json_to_array($config);
print_r($config);


Output:
Array
(
    [type] => Array
        (
            [category] => admin
            [page] => page
        )

    [say] => Hello
    [php] => Array
        (
            [say] => no
            [type] => Array
                (
                    [category] => admin
                    [page] => page
                )

            [gran] => Array
                (
                    [name] => Hi
                )

        )

)


Create a new paste based on this one


Comments: