首页 > 解决方案 > 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

外部回声的输出(通过返回传回的数据)是空白的。

任何人可以提供的帮助将不胜感激,提前感谢您所做的一切。

标签: phpjsonfunctionreturn-valueld

解决方案


推荐阅读