将字符串转换为数组并在php中获取值

时间:2014-04-11 18:53:59

标签: php

我有一个完整的字符串我怎么能得到它的一部分并插入数组这是我在php中的字符串

[{"albumid":"ASaBFzCtl8","albumname":"anni","type":"3","access":"2","itemcount":"2"},{"albumid":"EmgsZ43ehT","albumname":"testalbum","type":"1","access":"1","itemcount":"0"},{"albumid":"Jf4H4SvFGk","albumname":"test2album","type":"3","access":"1","itemcount":"0"},{"albumid":"k3pacBSmIl","albumname":"testalbumpvt","type":"3","access":"2","itemcount":"0"}]

我想要类似于此的东西

$value1 = $array[0];
// {"albumid":"ASaBFzCtl8","albumname":"anni","type":"3","access":"2","itemcount":"2"}

这样可以得到像这样的每个值

$value1 = $array[0]['albumid'];
 // ASaBFzCtl8

1 个答案:

答案 0 :(得分:3)

是使用json_decode()

$j = '[{"albumid":"ASaBFzCtl8","albumname":"anni","type":"3","access":"2","itemcount":"2"},{"albumid":"EmgsZ43ehT","albumname":"testalbum","type":"1","access":"1","itemcount":"0"},{"albumid":"Jf4H4SvFGk","albumname":"test2album","type":"3","access":"1","itemcount":"0"},{"albumid":"k3pacBSmIl","albumname":"testalbumpvt","type":"3","access":"2","itemcount":"0"}]';

$data = json_decode($j,true);

您可以使用循环将数据读取为

foreach($data as $key=>$val){
 echo $val["albumid"]."<br />";
}

上面只会得到你能从这个数组中读到的任何专辑。