将键与值分开

时间:2019-05-24 06:28:25

标签: php

我建立了一个新站点,并希望解决该问题。 我在mysql表中有此文本

{"200":"5","220":"0","65":"4","80":"0","199":"1","197":"1","198":"0","257":"4","223":"0"}

我想通过php获取ID及其值 例如

$id[200] = 5

1 个答案:

答案 0 :(得分:0)

您可以使用json_decode()将JSON字符串转换为数组。由于您的JSON字符串包含数字索引,因此您需要使用大括号来获取值

$str = '{"200":"5","220":"0","65":"4","80":"0","199":"1","197":"1","198":"0","257":"4","223":"0"}';
$array = json_decode($str);
echo $array->{'200'};

或者您可以在json_decode中将第二个参数添加为true以将值作为普通数组获取

$array = json_decode($str,true);
echo $array[200];

这是现场demo