我知道PHP与Json并不是最友好的语言,但我正在尝试做一些简单的事情。我需要遍历一个Json文件,修改一些元素并保存它。
我正在使用RecursiveIteratorIterator
,它可以很好地遍历数组。
我修改了一些元素(对它们进行翻译,可以很好地工作),然后使用翻译对其进行更新。
该代码用于prsng Json文件,而无需事先了解其节点。
Json:
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": [
"GML",
"XML"
]
},
"GlossSee": "markup"
}
}
}
}
}
这是代码:
$json = json_decode(file_get_contents('file.json'), TRUE);
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator($json),
RecursiveIteratorIterator::SELF_FIRST);
foreach ($jsonIterator as $key => $val) {
if(!is_array($val)) {
$xlation = mt($val,$lang, $apikey); //this works fine
$json[$key] = $xlation;
}
}
file_put_contents('translation.json', json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
这是结果文件,在其中添加内容而不是更新内容。
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": [
"GML",
"XML"
]
},
"GlossSee": "markup"
}
}
}
},
"title": "S",
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Linguaggio di marcatura generalizzato standard",
"Acronym": "SGML",
"Abbrev": "ISO 8879: 1986",
"para": "Un linguaggio di meta-markup, utilizzato per creare linguaggi di markup come DocBook.",
"0": "GML",
"1": "XML",
"GlossSee": "markup"
}
答案 0 :(得分:2)
您每次都在根$json
变量上设置密钥,因此它可以有效地展平它并覆盖可能再次出现的所有密钥。您必须以某种方式跟踪嵌套的键级别。
这是一个recursive示例,下面没有迭代器。我刚刚将JSON中的所有值都大写,然后将它们小写以模拟您的翻译函数调用。
function process(array $element): array {
$result = [];
foreach ($element as $key => $value) {
if (is_array($value)) {
$result[$key] = process($value); // recurse
} else {
$result[$key] = strtolower($value); // simulating "mt" translation function with "strtolower"
}
}
return $result;
}
$json = <<<JSON
{
"glossary": {
"title": "EXAMPLE GLOSSARY",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "STANDARD GENERALIZED MARKUP LANGUAGE",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A META-MARKUP LANGUAGE, USED TO CREATE MARKUP LANGUAGES SUCH AS DOCBOOK.",
"GlossSeeAlso": [
"GML",
"XML"
]
},
"GlossSee": "MARKUP"
}
}
}
}
}
JSON;
$array = json_decode($json, true);
$result = process($array);
echo json_encode($result, JSON_PRETTY_PRINT);
{ "glossary": { "title": "example glossary", "GlossDiv": { "title": "s", "GlossList": { "GlossEntry": { "ID": "sgml", "SortAs": "sgml", "GlossTerm": "standard generalized markup language", "Acronym": "sgml", "Abbrev": "iso 8879:1986", "GlossDef": { "para": "a meta-markup language, used to create markup languages such as docbook.", "GlossSeeAlso": [ "gml", "xml" ] }, "GlossSee": "markup" } } } } }