Hello I am working with Bing web search API and need some help with translating JSON into html using PHP. So I wabt to be able to print selective staff using foreach() and if() statements. I have looked uo on other entris but I cannot figure out what thw problem is.
Here is an JSON example:
{
"_type": "SearchResponse",
"queryContext": {
"originalQuery": "Microsoft Cognitive Services"
},
"webPages": {
"webSearchUrl": "https://www.bing.com/search?q=Microsoft+Cognitive+Services",
"totalEstimatedMatches": 2320000,
"value": [
{
"id": "https://api.cognitive.microsoft.com/api/v7/#WebPages.0",
"name": "Cognitive Services | Microsoft Azure",
"url": "https://azure.microsoft.com/es-es/services/cognitive-services/",
"displayUrl": "https://azure.microsoft.com/es-es/services/cognitive-services",
"snippet": "Agregue funcionalidades de visiu00f3n, voz, lenguaje y conocimiento a sus aplicaciones mediante las API de inteligencia artificial de Cognitive Services.",
"dateLastCrawled": "2017-11-22T05:45:00.0000000Z"
},
{
"id": "https://api.cognitive.microsoft.com/api/v7/#WebPages.1",
"name": "Microsoft Cognitive Services: Componentes del Servicio ...",
"url": "https://azure.microsoft.com/es-es/support/legal/cognitive-services-components/",
"displayUrl": "https://azure.microsoft.com/es-es/support/legal/cognitive-services...",
"snippet": "Revise el uso de los tu00e9rminos de Microsoft Cognitive Services: Componentes del Servicio.",
"dateLastCrawled": "2017-11-21T02:34:00.0000000Z"
},
{
"id": "https://api.cognitive.microsoft.com/api/v7/#WebPages.2",
"name": "Microsoft Trust Center | Microsoft Cognitive Services",
"url": "https://www.microsoft.com/en-us/trustcenter/cloudservices/cognitiveservices",
"displayUrl": "https://www.microsoft.com/.../cloudservices/cognitiveservices",
"snippet": "Microsoft Cognitive Services is a collection of intelligent APIs that allow systems to understand and interpret peopleu2019s needs by using natural methods ...",
"dateLastCrawled": "2017-11-18T16:46:00.0000000Z"
},
{
"id": "https://api.cognitive.microsoft.com/api/v7/#WebPages.3",
"name": "Cognitive Services | Microsoft Azure",
"url": "https://azure.microsoft.com/en-us/services/cognitive-services/",
"displayUrl": "https://azure.microsoft.com/en-us/services/cognitive-services",
"snippet": "Add vision, speech, language and knowledge capabilities to your apps with artificial intelligence APIs from Cognitive Services. Explore our APIs today.",
"dateLastCrawled": "2017-11-22T02:20:00.0000000Z"
},
{
"id": "https://api.cognitive.microsoft.com/api/v7/#WebPages.4",
"name": "Cognitive Service Try experience | Microsoft Azure",
"url": "https://azure.microsoft.com/en-us/try/cognitive-services/",
"displayUrl": "https://azure.microsoft.com/en-us/try/cognitive-services",
"snippet": "Microsoft Cognitive Services Try experience lets you build apps with powerful algorithms using just a few lines of code through a 30 day trial.",
"dateLastCrawled": "2017-11-21T02:42:00.0000000Z"
},
{
"id": "https://api.cognitive.microsoft.com/api/v7/#WebPages.5",
"name": "Cognitive Services Directory | Microsoft Azure",
"url": "https://azure.microsoft.com/en-us/services/cognitive-services/directory/",
"displayUrl": "https://azure.microsoft.com/.../services/cognitive-services/directory",
"snippet": "Learn more about Cognitive Services and manage them in the Azure cloud, or test them with temporary access.",
"dateLastCrawled": "2017-11-21T21:14:00.0000000Z"
},
{
"id": "https://api.cognitive.microsoft.com/api/v7/#WebPages.6",
"name": "What is Microsoft Cognitive Services? | Microsoft Docs",
"url": "https://learn.microsoft.com/en-us/azure/cognitive-services/Welcome",
"displayUrl": "https://learn.microsoft.com/en-us/azure/cognitive-services/Welcome",
"snippet": "Microsoft Cognitive Services is a set of APIs, SDKs, and services that you can use with Microsoft Azure that make applications more intelligent, engaging ...",
"dateLastCrawled": "2017-11-21T17:40:00.0000000Z",
"searchTags": [
{
"name": "search.ms_sitename",
"content": ""Docs"; docs"
},
{
"name": "search.ms_docsetname",
"content": ""azure-documents"; azure; documents"
},
{
"name": "search.ms_product",
"content": ""Azure"; azure"
}
]
},
{
"id": "https://api.cognitive.microsoft.com/api/v7/#WebPages.7",
"name": "Microsoft Cognitive Services",
"url": "https://westus.dev.cognitive.microsoft.com/docs/services/TextAnalytics.V2.0/operations/56f30ceeeda5650db055a3c7",
"displayUrl": "https://westus.dev.cognitive.microsoft.com/docs/services/Text...",
"snippet": "Text Analytics API. The Text Analytics API is a suite of text analytics web services built with best-in-class Microsoft machine learning algorithms.",
"dateLastCrawled": "2017-11-22T13:25:00.0000000Z"
}
]
}
I would like to print some stuff inside “webPages”, I am using the following php code:
$links = json_decode($json, TRUE);
foreach ($links['webPages'] as $j => $v) {
if($j=="value"){
foreach ($v as $k => $w) {
print $k . ": " . $w;
}
}
}
I am getting the following error:
Notice: Array to string conversion
And it prints:
0: Array
1: Array
...
7: Array
Thanks beforehand.
2
Answers
Use
vardump
instad ofprint
I personally prefer using
print_r
Do you intend this??
Also in this way there a foreach in less 🙂