递归搜索多维数组时获取第一个匹配键的值
private function find($needle, $haystack) {
foreach ($haystack as $name => $file) {
if ($needle == $name) {
return $file;
} else if(is_array($file)) { //is folder
return $this->find($needle, $file); //file is the new haystack
}
}
return "did not find";
}
此方法在关联数组中搜索特定键并返回与其关联的值。递归有一些问题。有什么线索吗?
private function find($needle, $haystack) {
foreach ($haystack as $name => $file) {
if ($needle == $name) {
return $file;
} else if(is_array($file)) { //is folder
return $this->find($needle, $file); //file is the new haystack
}
}
return "did not find";
}
This method searches for a specific key in an associative array and returns the value associated with it. There's some problem with the recursion. Any clue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
也许它对于旧版本的 PHP 来说有点杀伤力,但是对于 >=5.6(特别是 7.0),我毫无疑问会完全使用它。
此外,从 PHP 5.6 开始,使用生成器,您可以轻松迭代通过过滤器的所有元素,而不仅仅是第一个:
Maybe it was overkill with old versions of PHP, but with >=5.6 (specially with 7.0) I would totally use this without doubt.
Also, as of PHP 5.6, with generators you can easily iterate over all elements which pass the filter, not only the first one:
您需要通过返回 false 来停止递归深度搜索,然后在函数中检查它。
您可以在此链接中找到更多函数示例(例如使用 RecursiveArrayIterator 等):
http://php.net/manual/en/function.array-search。 php
You need to stop the recursive deep search, by return false and then check it in the function.
You can find more examples of functions (like using RecursiveArrayIterator and more) in this link:
http://php.net/manual/en/function.array-search.php
xPheRe 提供的答案非常有帮助,但并没有完全解决我的实现中的问题。我们的数据结构中有多个嵌套关联数组,并且任何给定的键可能会多次出现。
为了满足我们的目的,我需要实现一个在遍历整个结构时更新的持有者数组,而不是在第一个匹配时返回。真正的工作是由另一位海报提供的,但我想说声谢谢并分享我必须涵盖的最后一步。
The answer provided by xPheRe was extremely helpful, but didn't quite solve the problem in my implementation. There are multiple nested associative arrays in our data structure, and there may be multiple occurrences of any given key.
In order to suit our purposes, I needed to implement a holder array that was updated while traversing the entire structure, instead of returning on the first match. The real work was provided by another poster, but I wanted to say thanks and share the final step that I had to cover.
我刚刚经历过类似的问题,这对我有用:
这将返回一个数组,其中包含在多维数组中找到的所有匹配键的值。我使用电子邮件 API 动态生成的数组对此进行了测试。在多个匹配的情况下,您只需要创建一个简单的 foreach 循环即可根据需要对数组进行排序。
我注意到我犯的主要错误是在应该使用 if-if 条件时使用 if-ifelse 条件。有任何问题或批评都欢迎,干杯!
I just been through a similar issue and here's what worked for me:
This is going to return an array containing the value of all the matching keys it found in the multidimensional array. I tested this with arrays dinamically generated by an e-mail API. In the case of multiple matches, you just need to create a simple foreach loop to sort the array however you want.
I noticed the main mistake I was making was using if-ifelse conditions when I should be using if-if conditions. Any questions or criticism are very welcome, cheers!
我最近在处理 Yii2 查询对象时遇到了同样的问题。
您的函数不起作用的原因是返回操作在这里不起作用。只需传递一个引用参数来存储值,然后就可以做任何你想做的事情。
正如您所看到的,这是一个简单的 PHP 函数,不依赖于任何库。所以我认为上面列出的所有答案都值得一提。
I recently came across the same issue, when dealing with Yii2 query object.
The reason your function didn't work is that the return action doesn't work here. Just pass a reference parameter to store the value, and do whatever you want afterwards.
As you can see, this is a simple PHP function doesn't rely on any library. So I think its worth to mention with all the answer listed above.
如果键重复并且仅返回第一个值,上面的最佳解决方案会错过这种情况,这里我获取数组中的所有值: (演示)
The best solution above misses the case if the key is repeated and only returns the first value, here I get all the values in an array instead: (Demo)
这是我的解决方案:
返回找到的值的数组,如果找不到键,则返回一个空数组。
如果您只需要返回找到的第一个值,可以使用:
返回找到的第一个值。
如果未找到密钥,则返回
false
。具有以下数组的示例:
第一个函数将返回
["haha","hoho","hehe","huhu"]
,第二个函数将返回"haha"
Here's my solution:
Returns an array of the value(s) found, if the key isn't found then an empty array is returned.
If you only need to return the first value it finds you can use:
Returns the first value found.
If the key isn't found then
false
is returned.Example with the following array:
First function would return
["haha","hoho","hehe","huhu"]
, second one would return"haha"
试试这个:
try this: