php - PHP函数通过return传递时不返回任何信息,但会在函数内愉快地回显
问题描述
我想知道是否有人可以帮助我解决我正在编写的功能。基本上我正在尝试在一些 JSON-LD 数据中搜索特定键。JSON-LD 数据已使用 json_decode 分配给数组。数组看起来像这样,但可能有不同的格式和结构:
{
"@context":"https://schema.org",
"@graph":[
{
"@type":"Organization",
"@id":"https://www.web.site/#organization",
"name":"Some Name",
"url":"https://www.web.site/",
"sameAs":[
"https://www.facebook.com/website",
"https://www.linkedin.com/company/website",
"https://twitter.com/website"
],
"logo":{
"@type":"ImageObject",
"@id":"https://www.web.site/#logo",
"inLanguage":"en",
"url":"https://web.site/logo.svg",
"width":200,
"height":100,
"caption":"Some Name"
},
"image":{
"@id":"https://www.web.site/#logo"
}
},
{
"@type":"WebSite",
"@id":"https://www.web.site/#website",
"url":"https://www.web.site/",
"name":"Some Name",
"inLanguage":"en",
"description":"Some description here.",
"publisher":{
"@id":"https://www.web.site/#organization"
},
"potentialAction":[
{
"@type":"SearchAction",
"target":"https://www.web.site/?s={search_term_string}",
"query-input":"required name=search_term_string"
}
]
},
{
"@type":"WebPage",
"@id":"https://www.web.site/#webpage",
"url":"https://www.web.site/",
"name":"Some Name",
"isPartOf":{
"@id":"https://www.web.site/#website"
},
"inLanguage":"en",
"about":{
"@id":"https://www.web.site/#organization"
},
"datePublished":"2017-01-01T21:21:21+00:00",
"dateModified":"2017-01-01T21:21:21+00:00",
"description":"Some description here.",
"potentialAction":[
{
"@type":"ReadAction",
"target":[
"https://www.web.site/"
]
}
]
}
]
}
我编写的查找密钥的函数如下所示:
function findArrayKey($array, $find) {
foreach($array as $key => $value) {
if (is_array($value)) {
if((string)$key == $find){
foreach($value as $key => $value) {
$info .= $value;
}
} else {
findArrayKey($value,$find);
}
}
}
echo $info; // prints the info
return $info; // returns nothing
}
echo findArrayKey($myarray,$keyimlookingfor);
底部的 echo findArrayKey 行什么也没做(我也尝试过 var_dump 和 print_r 仍然一无所获)但函数内的 echo 工作得很好。
函数内回显的输出为:
https://www.facebook.com/websitehttps://www.linkedin.com/company/websitehttps://twitter.com/website
外部回声的输出(通过返回传回的数据)是空白的。
任何人可以提供的帮助将不胜感激,提前感谢您所做的一切。
解决方案
推荐阅读
- sql - SQL - 删除重复行而不保留原始行
- back4app - 带有java的Android工作室
- pine-script - 根据其他输入选择显示/隐藏输入字段
- python - Face_Recognition 加载编码文件时出现问题
- asp.net-core - AWS Lambda 与 Kestrel .NET Core 性能对比
- python - 将文件从芹菜任务发送到烧瓶路线
- javascript - Javascript - 如何返回数组中最大的四个数字?
- python - Linux:通过文件夹目录的一部分重命名文件并移动文件
- python - 算法:将n个元素分配到m个桶中,使得桶中元素的最大和最小
- c++ - MPI_Send() 的标签可以是长整数吗?