欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

截取多个JSON的参数值遇到问题。

程序员文章站 2022-06-07 11:45:22
...
POST获取的JSON数据 [{"Name":"PM","Value":"224"},{"Name":"WD","Value":"25"},{"Name":"SD","Value":"34"},]

如何截取 Name=WD 的Value?
求代码。


回复讨论(解决方案)

最简单的,循环查找呗。

$json = '[{"Name":"PM","Value":"224"},{"Name":"WD","Value":"25"},{"Name":"SD","Value":"34"}]';$jsonArr = json_decode($json) ;foreach ($jsonArr as  $value) {	if($value->Name == 'WD'){		echo $value->Value."
"; }}

$s = '[{"Name":"PM","Value":"224"},{"Name":"WD","Value":"25"},{"Name":"SD","Value":"34"}]';$t = array_filter(json_decode($s), function($v) { return $v->Name == 'WD'; });echo current($t)->Value; //25