将JSON字符串数组转换为PHP数组

时间:2014-04-13 04:24:51

标签: php

我在PHP中有一个JSON数组:

$array={"definitions":[
    {"text":"Informal One who is proficient at using or programming a computer; a computer buff.",
     "attribution":"from The American Heritage\u00ae Dictionary of the English Language, 4th Edition"
    },
    {"text":"Informal One who uses programming skills to gain illegal access to a computer network or file.",
     "attribution":"from The American Heritage\u00ae Dictionary of the English Language, 4th Edition"
    },
    {"text":"Informal One who enthusiastically pursues a game or sport: a weekend tennis hacker. ",
     "attribution":"from The American Heritage\u00ae Dictionary of the English Language, 4th Edition"
    },
    {"text":"See hackie.",
     "attribution":"from The American Heritage\u00ae Dictionary of the English Language, 4th Edition"
    }
]}

无论我尝试什么,我都无法访问这些元素。我需要访问这样的元素: $array[0]["definitions"][0]["text"]

2 个答案:

答案 0 :(得分:0)

尝试使用内置库http://www.php.net/manual/en/function.json-decode.php

或者,如果您运行的是旧版本的PHP,我使用PEAR Servers_JSON(http://pear.php.net/package/Services_JSON

答案 1 :(得分:0)

你可以做到

$data = json_decode($your_json_string, TRUE);

第二个参数将解码的json字符串转换为关联数组。

例如

json_decode('{"foo":"bar"}', true); // returns array("foo" => "bar")