简体   繁体   English

如何通过JSON搜索?

[英]How to search through a JSON?

I'm trying to search through this Json array using PHP: 我正在尝试使用PHP搜索此Json数组:

{"count":"2","items":{"milestone":[{"id":"3107","username":"TomSmith1","userid":"1620602","date":"2012-01-12 16:49:26","projectid":"804","projectname":"TEST PROJECT","reason":"Partial payment for project","amount":"25","currency":"1"},{"id":"3111","username":"HarryCole","userid":"1620603","date":"2012-01-13 16:42:39","projectid":"804","projectname":"TEST PROJECT 2","reason":"Partial payment for project","amount":"10","currency":"1"}]}}

I want to search through this to find the "id", which has the project name "TEST PROJECT 2" and the username "HarryCole". 我想在其中搜索“ id”,其名称为“ TEST PROJECT 2”,项目名称为“ HarryCole”。 I've attempted this using a foreach loop but haven't succeeded. 我已经尝试过使用foreach循环,但是没有成功。 Can anyone point me in the right direction? 谁能指出我正确的方向? Thanks. 谢谢。

您想在字符串上使用json_decode($json, true)将其从JSON转换为PHP表示法(关联数组)。

You're going to need http://us2.php.net/json_decode and also a foreach loop like 您将需要http://us2.php.net/json_decode和一个foreach循环,例如

foreach($json_decoded as $key=>$value) {
     // Check for id / values
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM