The Little Guy Posted December 23, 2009 Share Posted December 23, 2009 How can I order an array by relevance to another string? Lets say I have an array with the following: - what does php2 stand for? - what does html stand for? - what does php stand for? - what does js stand for? - what does css stand for? - what does php5 stand for? and say the string to search for is: "what does p" the returned results from the array would then be: - what does php stand for? - what does php2 stand for? - what does php5 stand for? I hope this makes sense, any suggestions how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/186124-search-an-array-for-results/ Share on other sites More sharing options...
rajivgonsalves Posted December 23, 2009 Share Posted December 23, 2009 here you go, hope its helpful <script type="text/javascript"> var array = new Array("what does php2 stand for?","what does html stand for?","what does php stand for?","what does js stand for?","what does css stand for?","what does php5 stand for?"); var result = new Array(); var str = "what does p"; var regExp = new RegExp("^"+str); for(i=0;i<array.length;i++) { if (regExp.test(array[i])) { result.push(array[i]); } } alert(result); </script> Quote Link to comment https://forums.phpfreaks.com/topic/186124-search-an-array-for-results/#findComment-982934 Share on other sites More sharing options...
The Little Guy Posted December 24, 2009 Author Share Posted December 24, 2009 Hmm... The loop works, the alert that is in the loop works but the if() inside the loop does not.... // suggestions array is generated from ajax var result = new Array(); var str = ibox.vaule; // ibox.value comes from a textbox var regExp = new RegExp("^"+str); for(var i in suggestions){ alert(suggestions[i]); if(regExp.test(suggestions[i])){ result[i] = suggestions[i]; } } Is something wrong? Quote Link to comment https://forums.phpfreaks.com/topic/186124-search-an-array-for-results/#findComment-983478 Share on other sites More sharing options...
rajivgonsalves Posted December 24, 2009 Share Posted December 24, 2009 I think your code should be, it all depends on whats in str, it just a simple regular expression checking for the text at the start of each expression. // suggestions array is generated from ajax var result = new Array(); var str = ibox.value; // ibox.value comes from a textbox var regExp = new RegExp("^"+str); for (var i in suggestions){ alert(suggestions[i]); if(regExp.test(suggestions[i])){ result[i] = suggestions[i]; } } Quote Link to comment https://forums.phpfreaks.com/topic/186124-search-an-array-for-results/#findComment-983517 Share on other sites More sharing options...
The Little Guy Posted December 24, 2009 Author Share Posted December 24, 2009 The problem was that I spelled value wrong see: var str = ibox.vaule; The code you first posted worked perfect! It was just my little spelling error Thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/186124-search-an-array-for-results/#findComment-983782 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.