使用递归方法将JSON转换为数组?

lau*_*kok 1 php recursion json php-5.3

我正在尝试将数组内的json字符串转换为数组,

$config = array(
    "type"  => '{"category":"admin","page":"page"}',
    "say"     => "Hello",
    "php"   => array(
        "say"     => "no",
        "type"  => '{"category":"admin","page":"page"}',
        "gran"  =>array(
            "name" => "Hi"
        )
    )
);
Run Code Online (Sandbox Code Playgroud)

我的工作代码

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($array) : $value;
        }

        return $array;
    }
}
Run Code Online (Sandbox Code Playgroud)

没有 递归方法的情况下,它可以正常工作,但是如我在上面的代码中所见,在我要进行递归时会中断,

$object = new objectify();
$config = $object->json_to_array($config);
print_r($config);
Run Code Online (Sandbox Code Playgroud)

错误信息,

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 2048 bytes) in C:\wamp\www\test\2012\php\set_variable.php on line 79
Run Code Online (Sandbox Code Playgroud)

我只是想得到这个结果,

Array
(
    [type] => Array
        (
            [category] => admin
            [page] => page
        )
    [say] => Hello
        (
            [say] => no
            [type] => {"category":"admin","page":"page"}
            [gran] => Array
                (
                    [name] => Hi
                )

        )

)
Run Code Online (Sandbox Code Playgroud)

编辑:

$config = 'type={"category":"admin","page":"page"}&text_editor={"name":"mce-basic"}&parent_id=self&subtitle=true&description=true&content_1=true&script_1=true&primary_image=true';
parse_str($config,$array);
print_r($array);
Run Code Online (Sandbox Code Playgroud)

结果,

Array
(
    [type] => {"category":"admin","page":"page"}
    [text_editor] => {"name":"mce-basic"}
    [parent_id] => self
    [subtitle] => true
    [description] => true
    [content_1] => true
    [script_1] => true
    [primary_image] => true
)
Run Code Online (Sandbox Code Playgroud)

Mar*_*eed 5

快速解决方案:

$full_array = array_map('json_decode', $array);
Run Code Online (Sandbox Code Playgroud)

如果并非所有参数都是JSON,并且您想要实际的数组而不是stdClass对象,则可能必须这样做:

function json_decode_array($input) { 
  $from_json =  json_decode($input, true);  
  return $from_json ? $from_json : $input; 
}
$full_array = array_map('json_decode_array', $array);
Run Code Online (Sandbox Code Playgroud)

如果您在JSON之外具有更多级别的嵌套数组,则必须进行自己的递归。试试这个版本的objectify:

class objectify
{
  public function json_mapper($value, $recursive = true) {
    if (!empty($value) && is_string($value) &&
        $decoded = json_decode($value, true)) {
      return $decoded;
    } elseif (is_array($value) && $recursive) {
      return array_map('objectify::json_mapper', $value);
    } else {
      return $value;
    }
  }

  // currying, anyone?
  public function json_mapper_norecurse($value) { 
     return objectify::json_mapper($value, false);
  }

  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);
    }

    return array_map(
      $recursive ? 'objectify::json_mapper' 
                 : 'objectify::json_mapper_norecurse', $array);
  }
}
Run Code Online (Sandbox Code Playgroud)

这对您的两组样本数据都适用。