PHP,JSON-仅接收具有准确值的字段

时间:2018-11-15 10:52:39

标签: php json

使用以下代码,我从json-list中获取值

$data = json_decode($result, true);
foreach ($data['items'] as $key => $value) {
echo $value['classification_id'].','.$value['classification_name'];

结果是

5,Standorte (Leihgeber)6,Objekttypen9,Museen/Sammlungen
(Webgate)10,Vervielfältigungen von11,Vorlagen-Format 112,Vorlagen-Typ
115,Maßangaben16,Geografischer Bezug17,Vorlagen-Status
118,Vorlagen-Status 219,Vorlagen-Status 320,Vorlagen-Status
421,Vorlagen-Format 222,Vorlagen-Format 323,Vorlagen-Format
424,Vorlagen-Typ 225,Vorlagen-Typ 326,Vorlagen-Typ
429,Künstler30,Fotografen31,Personen32,Stichworte/Keywords36,39,Copyright_Zusaetze
(Standort-Zusatz)42,Test_PHP

现在我只想获取“ classification_id”和“ classification_name”字段,其中“ classification_id”字段的值为“ 42”(42,Test_PHP)。我该怎么做?

杰森的例子

23  
classification_id   39
classification_name "Copyright_Zusaetze (Standort-Zusatz)"
classification_dtcreate "2016-02-10T14:00:12+01:00"
classification_dtupdate "2016-09-15T17:44:35+02:00"
24  
classification_id   42
classification_name "Test_PHP"
classification_dtcreate "2018-10-10T16:06:57+02:00"
classification_dtupdate "2018-10-18T11:09:51+02:00"

echo $ result

{"request":{"date":"2018-11-15T12:00:42+01:00",
            "uid":"20181115-120042-66b3dfab25a8a11180a46313ac401b3c"
            },
"offset":0,
"limit":5000,
"totalCount":25,
"items":[
            {"classification_id":5,
            "classification_name":"Standorte (Leihgeber)",
            "classification_dtcreate":"2013-09-17T13:58:58+02:00",
            "classification_dtupdate":"2015-02-13T15:12:51+01:00"
            },
            {"classification_id":6,
            "classification_name":"Objekttypen",
            "classification_dtcreate":"2013-09-19T15:18:26+02:00",
            "classification_dtupdate":"2013-09-19T15:30:38+02:00"
            },
            {"classification_id":9,
            "classification_name":"Museen\/Sammlungen (Webgate)",
            "classification_dtcreate":"2013-10-21T17:31:12+02:00",
            "classification_dtupdate":"2017-08-15T07:55:02+02:00"
            },
            {"classification_id":10,
            "classification_name":"Vervielf\u00e4ltigungen von",
            "classification_dtcreate":"2014-01-08T14:32:55+01:00",
            "classification_dtupdate":"2014-05-14T10:50:12+02:00"
            },{"classification_id":11,"classification_name":"Vorlagen-Format 1","classification_dtcreate":"2014-01-08T15:01:10+01:00","classification_dtupdate":"2014-06-11T15:41:31+02:00"},{"classification_id":12,"classification_name":"Vorlagen-Typ 1","classification_dtcreate":"2014-01-08T15:23:40+01:00","classification_dtupdate":"2014-05-14T11:02:39+02:00"},{"classification_id":15,"classification_name":"Ma\u00dfangaben","classification_dtcreate":"2014-04-10T09:42:12+02:00","classification_dtupdate":"2014-04-10T09:42:12+02:00"},{"classification_id":16,"classification_name":"Geografischer Bezug","classification_dtcreate":"2014-04-30T13:35:10+02:00","classification_dtupdate":"2014-04-30T13:35:10+02:00"},{"classification_id":17,"classification_name":"Vorlagen-Status 1","classification_dtcreate":"2014-05-14T10:39:27+02:00","classification_dtupdate":"2014-06-11T15:42:23+02:00"},{"classification_id":18,"classification_name":"Vorlagen-Status 2","classification_dtcreate":"2014-05-14T10:41:17+02:00","classification_dtupdate":"2014-06-11T15:42:30+02:00"},{"classification_id":19,"classification_name":"Vorlagen-Status 3","classification_dtcreate":"2014-05-14T10:41:55+02:00","classification_dtupdate":"2014-06-11T15:42:38+02:00"},{"classification_id":20,"classification_name":"Vorlagen-Status 4","classification_dtcreate":"2014-05-14T10:42:16+02:00","classification_dtupdate":"2014-06-11T15:42:47+02:00"},{"classification_id":21,"classification_name":"Vorlagen-Format 2","classification_dtcreate":"2014-05-14T10:50:25+02:00","classification_dtupdate":"2014-06-11T15:41:40+02:00"},{"classification_id":22,"classification_name":"Vorlagen-Format 3","classification_dtcreate":"2014-05-14T10:50:47+02:00","classification_dtupdate":"2014-06-11T15:41:48+02:00"},{"classification_id":23,"classification_name":"Vorlagen-Format 4","classification_dtcreate":"2014-05-14T10:51:21+02:00","classification_dtupdate":"2014-06-11T15:42:07+02:00"},{"classification_id":24,"classification_name":"Vorlagen-Typ 2","classification_dtcreate":"2014-05-14T11:02:44+02:00","classification_dtupdate":"2014-05-14T11:02:50+02:00"},{"classification_id":25,"classification_name":"Vorlagen-Typ 3","classification_dtcreate":"2014-05-14T11:03:17+02:00","classification_dtupdate":"2014-05-14T11:03:23+02:00"},{"classification_id":26,"classification_name":"Vorlagen-Typ 4","classification_dtcreate":"2014-05-14T11:03:50+02:00","classification_dtupdate":"2014-05-14T11:03:58+02:00"},{"classification_id":29,"classification_name":"K\u00fcnstler","classification_dtcreate":"2014-11-18T15:20:51+01:00","classification_dtupdate":"2018-11-05T11:09:42+01:00"},{"classification_id":30,"classification_name":"Fotografen","classification_dtcreate":"2015-01-15T13:28:57+01:00","classification_dtupdate":"2017-11-07T10:59:19+01:00"},{"classification_id":31,"classification_name":"Personen","classification_dtcreate":"2015-01-15T13:42:26+01:00","classification_dtupdate":"2015-12-16T11:22:20+01:00"},{"classification_id":32,"classification_name":"Stichworte\/Keywords","classification_dtcreate":"2015-01-22T15:29:00+01:00","classification_dtupdate":"2016-05-10T12:05:17+02:00"},{"classification_id":36,"classification_name":"","classification_dtcreate":"2015-05-06T09:32:20+02:00","classification_dtupdate":"2015-05-06T09:32:20+02:00"},{"classification_id":39,"classification_name":"Copyright_Zusaetze (Standort-Zusatz)","classification_dtcreate":"2016-02-10T14:00:12+01:00","classification_dtupdate":"2016-09-15T17:44:35+02:00"},{"classification_id":42,"classification_name":"Test_PHP","classification_dtcreate":"2018-10-10T16:06:57+02:00","classification_dtupdate":"2018-10-18T11:09:51+02:00"}]}

谢谢

1 个答案:

答案 0 :(得分:2)

如果classification_id是唯一值,则可以使用array_column()用该值重新索引数组。然后,您可以使用后面的键直接访问该数组。

$data = array_column($data['items'],null,'classification_id');

echo $data[42]['classification_name'];

给予...

Test_PHP