'{"category":"admin","page":"page"}', -6ren">
gpt4 book ai didi

php - 使用递归方法将json转换为数组?

转载 作者:行者123 更新时间:2023-12-03 22:54:52 25 4
gpt4 key购买 nike

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

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

return $array;
}
}

没有 也能正常工作递归方法但是当我想做 时会中断递归 正如你在我上面的代码中看到的,
$object = new objectify();
$config = $object->json_to_array($config);
print_r($config);

错误信息,
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

我只想得到这个结果,
Array
(
[type] => Array
(
[category] => admin
[page] => page
)
[say] => Hello
(
[say] => no
[type] => {"category":"admin","page":"page"}
[gran] => Array
(
[name] => Hi
)

)

)

编辑:
$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);

结果,
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
)

最佳答案

快速解决方案:

$full_array = array_map('json_decode', $array);

如果不是所有参数都是 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);

如果您在 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);
}
}

在您的两组样本数据上,这对我来说都很好。

关于php - 使用递归方法将json转换为数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10457883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com