在PHP递归函数中返回

我正在编写一个 PHP 递归函数来使用它们的值从数组中获取数据。所以这是我要构建的功能:


  function test($menu) {


    $url = "test.com/accounts/overview/";


    foreach($menu as $data) {


      if( is_array($data) and array_key_exists("t_link", $data) and $data["t_link"] === $url ) {


        return $data["t_icon"]; 


      } else if(is_array($data)) {


        test($data);


      }

    }

  }


echo test($menu);

从我的数组开始,第一个条件只有一次为真,并且必须return返回值并终止函数,不是吗?但为什么它没有返回?另外,如果我使用echo $data["t_icon"];而不是return $data["t_icon"];它显示正确的结果:fa fa-book。


这是我试图t_icon根据值获取值的数组t_link。条件是如果t_link值有test.com/accounts/overview/那么它将返回fa fa-book


$menu = array ();

$menu["Dashboard"] = array (

    "t_link"    => "test.com",

    "t_icon"    => "fa fa-dashboard"

  );

  $menu["Accounts"] = array (

    "t_link"    => "test.com/accounts",

    "t_icon"    => "fa fa-books"

  );

  $menu["Accounts"]["Overview"] = array (

    "t_link"    => "test.com/accounts/overview/",

    "t_icon"    => "fa fa-book"  <<-- This value I want to get

  );

我搜索了很多并得到这个我应该像这样在第二个条件内返回函数return test($data);。但它也不起作用。谢谢。


婷婷同学_
浏览 145回答 2
2回答

慕标5832272

您的代码不起作用,因为函数应该始终具有返回值 - 在递归中它是必需的:function test($menu) {&nbsp; &nbsp; $url = "test.com/accounts/overview/";&nbsp; &nbsp; foreach($menu as $data) {&nbsp; &nbsp; &nbsp; if( is_array($data) &&&nbsp;&nbsp; &nbsp; &nbsp; isset($data["t_link"]) &&&nbsp;&nbsp; &nbsp; &nbsp; $data["t_link"] === $url ) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $data["t_icon"];&nbsp;&nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; else if (is_array($data)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result = test($data); //Get result back from function&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //If it's NOT NULL call the function again (test($data))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //(down below returns null when looped through&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //everything recursively)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($result !== null) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $result;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return null;}这是实现您想要的 OOP 风格的另一种方法:该解决方案背后的想法是创建一个两级数组(不多也不少)并从该新数组中获取数据。class Searcher {&nbsp; &nbsp; private $new_arr = array();&nbsp; &nbsp; private $icon = '';&nbsp; &nbsp; //array_walk_recursive goes through your array recursively&nbsp; &nbsp; //and calls the getdata-method in the class. This method creates&nbsp; &nbsp; //a new array with strings (not arrays) from supplied $array ($menu in your case)&nbsp; &nbsp; public function __construct($array, $search_url) {&nbsp; &nbsp; &nbsp; &nbsp; array_walk_recursive($array, array($this, 'getdata'));&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $key = array_search($search_url, $this->new_arr['t_link']);&nbsp; &nbsp; &nbsp; &nbsp; $this->icon = $this->new_arr['t_icon'][$key];&nbsp; &nbsp; }&nbsp; &nbsp; public function geticon() {&nbsp; &nbsp; &nbsp; &nbsp; return $this->icon;&nbsp; &nbsp; }&nbsp; &nbsp; public function getdata($item, $key) {&nbsp; &nbsp; &nbsp; &nbsp; if (!is_array($item)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->new_arr[$key][] = $item;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }}//Implementation (usage) of above class$search = new Searcher($menu, 'test.com/accounts/overview/');$icon = $search->geticon();echo 'ICON=' . $icon; //Would echo out 'fa fa-book'进一步说明:基于你的$menu数组,该类将创建一个像这样的数组($this->new_arr):Array(&nbsp; &nbsp; [t_link] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => test.com&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => test.com/accounts&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [2] => test.com/accounts/overview/&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [t_icon] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => fa fa-dashboard&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => fa fa-books&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [2] => fa fa-book&nbsp; &nbsp; &nbsp; &nbsp; ))新数组中的所有键都相互关联。这是$this->new_arr[$key][] = $item; 在getdata()方法中设置的。这是基于这样一个事实,即数组中的t_link-keys 和-keys的数量必须相等。t_icon因为这:$this->new_arr[0]['t_link'] is related to $this->new_arr[0]['t_icon'];$this->new_arr[1]['t_link'] is related to $this->new_arr[1]['t_icon'];$this->new_arr[2]['t_link'] is related to $this->new_arr[2]['t_icon'];etc.. (not more in your example)有此代码时:$key = array_search($search_url, $this->new_arr['t_link']);如果您已提供给test.com/accounts/overview/,它将提供密钥2$search_url所以:$this->icon = $this->new_arr['t_icon'][$key];设置为fa fa-book

慕桂英3389331

因为你在函数内部调用函数,你必须返回函数的值,这里是修复:(在 test($data); 之前添加“return”)&nbsp;function test($menu) {&nbsp; &nbsp; $url = "test.com/accounts/overview/";&nbsp; &nbsp; foreach($menu as $data) {&nbsp; &nbsp; &nbsp; if( is_array($data) and array_key_exists("t_link", $data) and $data["t_link"] == $url ) {&nbsp; &nbsp; &nbsp; &nbsp; return $data["t_icon"];&nbsp;&nbsp; &nbsp; &nbsp; } else if(isset($data["Overview"])) {&nbsp; &nbsp; &nbsp; &nbsp; return test($data);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }此外,所有这三种情况,它们都是数组,因此return test($data);将停止 foreach 然后$menu["Accounts"]["Overview"]永远不会被检查。
打开App,查看更多内容
随时随地看视频慕课网APP