我正在使用基于API的此工具http://apps.evemilano.com/entities/。 这是我得到的JSON文件。并非每个“itemListElement”都具有相同的详细程度。我想用“描述”计算所有ItemList。
{
"@context": {
"@vocab": "http://schema.org/",
"goog": "http://schema.googleapis.com/",
"EntitySearchResult": "goog:EntitySearchResult",
"detailedDescription": "goog:detailedDescription",
"resultScore": "goog:resultScore",
"kg": "http://g.co/kg"
},
"@type": "ItemList",
"itemListElement": [
{
"@type": "EntitySearchResult",
"result": {
"@id": "kg:/g/121gpq6x",
"name": "Calogero Angelo Sacheli",
"@type": [
"Person",
"Thing"
],
"description": "Author"
},
"resultScore": 7.989742
},
{
"@type": "EntitySearchResult",
"result": {
"@id": "kg:/m/0z3rcdq",
"name": "Giovanni Sacheli",
"@type": [
"Thing",
"Person"
],
"url": "http://www.evemilano.com/"
},
"resultScore": 6.925036
},
{
"@type": "EntitySearchResult",
"result": {
"@id": "kg:/m/07zy0l",
"name": "Quarterback Princess",
"@type": [
"Movie",
"Thing"
],
"description": "1983 film",
"detailedDescription": {
"articleBody": "Quarterback Princess is a 1983 American made-for-television fact-based sports drama film by 20th Century Fox that chronicles the courage and determination of a teenage girl who struggles against sexism and fights to play on her high school football team. ",
"url": "https://en.wikipedia.org/wiki/Quarterback_Princess",
"license": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License"
}
},
"resultScore": 0.945328
}
]
}
并非每个实体都有一个描述,有3个实体,只有2个“描述”。我如何使用PHP只计算现有的“描述”?我需要打印“2”:)
谢谢。
答案 0 :(得分:0)
您可以使用json_decode
解码您的json有效负载:
$data = json_decode($json, true);
然后您可以迭代$data['itemListElement']
并检查是否定义了$item['result']['description']
:
$count = array_reduce($data['itemListElement'], function($count, $item) {
return isset($item['result']['description']) ? ++$count : $count;
}, 0);
echo $count; // 2