如何将数据放入数组?

时间:2012-06-11 15:47:52

标签: php

我有一个字段格式:

{"2G Network":"GSM 850","3G Network":"HSDPA 850"}

如何将数据格式化为数组:

array(["2G Network"]=>"GSM 850", ["3G Network"]=>"HSDPA 850");

如何实现这个想法

3 个答案:

答案 0 :(得分:3)

$data = '{"2G Network":"GSM 850","3G Network":"HSDPA 850"}';

$myArray = json_decode( $data, true ); //true to make it associative array

http://php.net/manual/en/function.json-decode.php

答案 1 :(得分:1)

您想使用json_decode()

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json, true));

array(5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

答案 2 :(得分:0)

$text = '{"2G Network":"GSM 850","3G Network":"HSDPA 850"}';
$myArray = json_decode($text, true);

使用:

echo $myArray['2G Network'];