我将假设您将 json 作为字符串:
$str = '{"ABCD":{"Hindi":"82"},"EFGH":{"English":"78"},"IJKL":{"Urdu":"82"},"MNOP":{"Sanskrit":"78"}}';
$str = str_replace("{", "array(", $str);
$str = str_replace("}", ")", $str);
$str = str_replace(":", ",", $str);
$obj = "";
$newstr = "";
for ($i = 0; $i < count($obj); $i++) {
$e = $obj[$i];
$zero = 0;
$one = 1;
$last = intval(count($obj)) - $one;
if (gettype($obj[$i]) == gettype(array(0,0))) {
//optional if to prevent the comma at the end.
if ($i != $last) {
$newstr = $newstr."$e[$zero] = $e[$one], <br/>\n"; //<br/> if displayed as html
} else {
$newstr = $newstr."$e[$zero] = $e[$one]";
}
} else {
$newstr = $newstr."$e : ";
}
}
echo $newstr;
我没有使用json_decode,因为我发现嵌套数组更容易用循环解析。
我知道你可以采取一些捷径来代替我做的事情(但复制更容易)。
另外,输出将是:
ABCD: Hindi = 82,
EFGH: English = 78,
IJKL: Urdu = 82,
MNOP: Sanskrit = 78
希望这会有所帮助!