在数组中搜索相关性
我正在用 PHP 做一个非常小的在线商店应用程序。所以我有一个 PHP 地图数组。我想在数组中搜索一个字符串(一个产品)。我查看了 PHP 中的 array_search ,它似乎只查找完全匹配。你们知道有更好的方法来实现这个功能吗?由于这只是我实际做的事情的一小部分,所以我希望有一些内置的东西。有什么想法吗?
谢谢!
编辑:该数组包含以下格式的“产品”:
[6] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 2000-YM
)
[Name] => Team Swim School T-Shirt
[size] => YM
[price] => 15
[group] => Team Clothing
[id] => 2000-YM
)
[7] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 3000-YS
)
[Name] => Youth Track Jacket
[size] => YS
[price] => 55
[group] => Team Clothing
[id] => 3000-YS
)
所以我想知道我可以进行诸如“团队”之类的搜索,它会返回我在这里看到的第一个项目。我根据名称进行搜索(同样,这只是一些小东西)。我知道我可以找到确切的字符串,如果找不到确切的项目,我只是停留在“最佳结果”上。效率很好,但不是必需的,因为我只有大约 50 个项目,所以即使我使用“慢”算法也不会花费太多时间。
I am doing a very small online store application in PHP. So I have an array of maps in PHP. I want to search for a string (a product) in the array. I looked at array_search in PHP and it seems that it only looks for exact match. Do you guys know a better way to do this functionality? Since this is a very small part of what I am actually doing, I was hoping that there was something built in. Any ideas?
Thanks!
EDIT: The array contains "products" in this format:
[6] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 2000-YM
)
[Name] => Team Swim School T-Shirt
[size] => YM
[price] => 15
[group] => Team Clothing
[id] => 2000-YM
)
[7] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 3000-YS
)
[Name] => Youth Track Jacket
[size] => YS
[price] => 55
[group] => Team Clothing
[id] => 3000-YS
)
So I was wondering I can do a search such as "Team" and it would return me first item seen here. I am basing the search on the Name (again this is just something small). I understand that I can find the exact string, I am just stuck on the "best results" if it cannot find the exact item. Efficiency is nice but not required since I only have about 50 items so even if I use a "slow" algorithm it won't take much time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
array_filter
允许您指定自定义函数来执行搜索。在您的例子中,一个简单的函数使用strpos()
来检查您的搜索字符串是否存在:或者,您可以使用匿名函数来帮助防止命名空间污染:
array_filter
lets you specify a custom function to do the searching. In your case, a simple function that usesstrpos()
to check if your search string is present:Alternatively, you could use an anonymous function to help prevent namespace contamination:
或者您可以使用 preg_match 代替 strpos 进行更灵活的搜索。
or you can use preg_match for more flexible search instead of strpos.
我认为 Marc B 的回答是一个很好的起点,但对我来说它有一些问题。例如,您必须知道“编译时”的 Needle 是什么,因为您无法动态更改该值。另外,如果针出现在字符串元素的开头,它会表现得好像根本不存在一样。因此,经过一些实验,我设法想出了解决这两个问题的方法。因此您不必为您想要使用的每个不同的针创建一个新函数。
它将被这样调用:
并且现在可以在代码其余部分的相同范围内访问“needle”,您可以将“needle”设置为您想要的任何其他字符串变量,包括用户输入。
I think Marc B's answer was a good starting point but for me it had some problems. Such as you have to know what the Needle is at "compile time" because you can't dynamically change that value. also if the needle appeared at the start of the string element it would act like it's not there at all. so after a little experimenting I manged to come up with a way around both problems. so you don't have to create a new function for every different needle your going to want to use anymore.
and it would be called like this:
and being as needle is now accessible in the same scope that the rest of the code is you can set needle to any other string variable you wanted, including user input.
不幸的是,搜索是计算机科学中最困难的事情之一。如果您基于文字字符串匹配或正则表达式 (regex) 构建搜索,您可能会发现您会对返回结果的相关性不满意。
如果您有兴趣卷起袖子尝试更复杂的解决方案,我会尝试 Zend 的 Lucene 实现(http://framework.zend.com/manual/en/zend.search.lucene.html )。我已经用它在网站上实现了搜索。虽然花了几天时间,但结果比 15 分钟的文字字符串匹配解决方案要好得多。
附言。这是一个示例: http://devzone.zend.com/article/91
Unfortunately, search is one of the more difficult things to do in computer science. If you build for search based on literal string matches or regular expressions (regex), you may find that you'll be unhappy with the relevance of the results that are returned.
If you're interested in rolling up your sleeves and getting a little dirty with a more sophisticated solution, I'd try Zend's Lucene implementation ( http://framework.zend.com/manual/en/zend.search.lucene.html ). I've implemented a search on a site with it. It took a few days, but the results were MUCH better than the 15 minute solution of literal string matching.
PS. Here's an example: http://devzone.zend.com/article/91
我有同样的问题,但我创建了一个函数来通过传递数组、键和值来搜索数组。
I have same Issue but i have created i function to search in array by passing the array, key and value.