自:
citys["bj"] = {bj:"Beijing"};
citys["han"] = {haikou:"Haikou",hainan:"Hainan",sanya:"Sanya",wzs:"Wuzhishan"};
要:
"bj" => array("bj"=>"Beijing");
"han" => array("haikou"=>"Haikou","hainan"=>"Hainan","sanya"=>"Sanya","wzs"=>"Wuzhishan");
谢谢!
答案 0 :(得分:4)
json_decode('{"bj":"Beijing"}', true);
但是对于这个功能很好,你需要正确的 json,键也被引号包围。
答案 1 :(得分:1)
您可以分两步解决此问题:
$temp = preg_replace('/(\w*?):("\w*?")/', '"$1"=>$2', $input);
$output = preg_replace('/citys\[("\w*?")\]\s*=\s*\{(.*?)\}/', '$1 => array($2)', $temp);
首先,您将所有haikou:"Haikou"
转换为"haikou"=>"Haikou"
。然后,您将citys["bj"] = {...};
转换为"bj" => array(...);
正则表达式是:
(\w*?):("\w*?")
citys\[("\w*?")\]\s*=\s*{(.*?)}