[英]Return inside PHP recursive function
I am writing a PHP recursive function to get data using their value form an array.我正在编写 PHP 递归 function 以使用它们的值从数组中获取数据。 So here is function what I am trying to build:
所以这是我要构建的 function :
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);
The first condition only true for one time as of my array and the return
must return the value & terminate the function, isn't it?.第一个条件仅在我的数组中一次为真,并且
return
必须返回值并终止 function,不是吗? But why it is noting return?但为什么它注意到回报? Also, if I use
echo $data["t_icon"];
另外,如果我使用
echo $data["t_icon"];
instead of return $data["t_icon"];
而不是
return $data["t_icon"];
it is showing the correct result: fa fa-book
.它显示了正确的结果:
fa fa-book
。
This is the array from where I am trying to get the t_icon
value based on the t_link
value.这是我试图根据
t_link
值获取t_icon
值的数组。 The condition is if t_link
value has test.com/accounts/overview/
then it will return the fa fa-book
条件是如果
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
);
I searched a lot of and got this that I should return the function inside the second condition like this return test($data);
我搜索了很多并得到了这个,我应该在第二个条件内返回 function,就像这样
return test($data);
. . But it also not working.
但它也不起作用。 Thank you.
谢谢你。
Because you call a function inside a function, you have to return the value of the function, here is the fix: (added "return" before test($data);)因为你在 function 中调用了 function,所以你必须返回 ZC1C425268E68385D1AB5074$C17A94F14Z 的值,这里是修复:(添加“return”之前(测试数据);)
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(isset($data["Overview"])) {
return test($data);
}
}
}
Also, all three cases, they both array so return test($data);
此外,所有这三种情况,它们都是数组,所以
return test($data);
will stop the foreach then $menu["Accounts"]["Overview"]
will never be check.将停止 foreach 然后
$menu["Accounts"]["Overview"]
将永远不会被检查。
Your code is not working because a function should always have a return value - in recursion it is required :您的代码不起作用,因为 function应该始终有一个返回值 - 在递归中它是必需的:
function test($menu) {
$url = "test.com/accounts/overview/";
foreach($menu as $data) {
if( is_array($data) &&
isset($data["t_link"]) &&
$data["t_link"] === $url ) {
return $data["t_icon"];
}
else if (is_array($data)) {
$result = test($data); //Get result back from function
//If it's NOT NULL call the function again (test($data))
//
//(down below returns null when looped through
//everything recursively)
//
if ($result !== null) {
return $result;
}
}
}
return null;
}
Here's another way of achieving what you want OOP-style:这是实现您想要的 OOP 风格的另一种方法:
The idea behind this solution is to create a two-level array (no more, no less nr of levels) and grab data from that new array.这个解决方案背后的想法是创建一个两级数组(不多,不少于 nr 个级别)并从该新数组中获取数据。
class Searcher {
private $new_arr = array();
private $icon = '';
//array_walk_recursive goes through your array recursively
//and calls the getdata-method in the class. This method creates
//a new array with strings (not arrays) from supplied $array ($menu in your case)
public function __construct($array, $search_url) {
array_walk_recursive($array, array($this, 'getdata'));
$key = array_search($search_url, $this->new_arr['t_link']);
$this->icon = $this->new_arr['t_icon'][$key];
}
public function geticon() {
return $this->icon;
}
public function getdata($item, $key) {
if (!is_array($item)) {
$this->new_arr[$key][] = $item;
}
}
}
//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'
Further explanation:进一步说明:
Based on your $menu
- array the class will create an array like this ( $this->new_arr
):根据您的
$menu
- 数组 class 将创建一个这样的数组( $this->new_arr
):
Array
(
[t_link] => Array
(
[0] => test.com
[1] => test.com/accounts
[2] => test.com/accounts/overview/
)
[t_icon] => Array
(
[0] => fa fa-dashboard
[1] => fa fa-books
[2] => fa fa-book
)
)
All the keys are related to eachother in the new array.新数组中的所有键都相互关联。 This is set with
$this->new_arr[$key][] = $item;
这是用
$this->new_arr[$key][] = $item;
设置的within the getdata()
method.在
getdata()
方法中。 This is based on the fact it must be equal number of t_link
-keys and t_icon
-keys in the array.这是基于数组中
t_link
-keys 和t_icon
-keys 的数量必须相等的事实。
Because of this:因为这:
$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)
When having this code:拥有此代码时:
$key = array_search($search_url, $this->new_arr['t_link']);
it would give the key 2 if you have supplied $search_url
to test.com/accounts/overview/如果您已将
$search_url
提供给test.com/accounts/overview/ ,它将给出密钥2
Therefore:所以:
$this->icon = $this->new_arr['t_icon'][$key];
is set to fa fa-book设置为 fa fa-book
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.