Portal Home > Knowledgebase > Articles Database > Searching array in PHP


Searching array in PHP




Posted by LittleApps-Nick, 02-05-2013, 02:53 AM
I am developing a FAQ (Frequently Asked Question) PHP script and I want to implement a way for users to search the FAQs. This is a small script so I have the categories and FAQs in an array as shown below: Now, what I want to do is take a search query from a form (using a GET request) and search this array for that search query. I was wondering how I might go about doing this? Would I have to parse the search query first so that the array can be searched? Would I be able to use the array_search(); function for this or would I have to write my own code to iterate through the array? I was also wondering if the FAQs are better to be stored using a different method (XML, SQL, etc) to do this?

Posted by sitesten, 02-05-2013, 03:53 AM
Dear ub3rst4r, It depends of your needs. It's always good to have the data inside a database. But it depends how much efford you're going to put on and how much you're familiar with database programming. In addition in particular scenarios you may want to simplify the whole application design and integrate this data in simple text files, XML files, or directly integrated within your code. Since your initial question is how to search through an array, I'm going to try helping you with it. Since the user may enter multiple keywords you have to search for each word separately, otherwise a result may not be found. Here's an example, the user search for: "red apples", but in your array it is written "Our apples are red". If you simply search for "red apples" no match will be found. Instead you should extract each single keyword, e.g. like: In addition array_search will not do the job for you. It will simply search for a value within the array that exactly match searched haystack. For example if your array contains the string "Our apples are red" buy you search for "apples", those will not match as they're actually different. So you have to do a custom search. Also you should make case insensitive search using a routine like stripos (strpos is case sensitive). Here's as simple example written from scratch (not verified or tested). It may not run at all, but just to have a basic idea.

Posted by salcapolupo, 02-05-2013, 03:55 AM
Hi, I think this is not the right way for doing this... you better give a chance to using MySQL, store your FAQ into a table i.e. FAQ(ID, title, content, ...) and insert/select your desired information. If you use as array you can always search into into - also using linear search with foreach construct - but I repeat, MySQL and database solutions in general seems very much easy & flexible.

Posted by Evixo, 02-05-2013, 11:52 AM
1 for SQL. But if you want to do it with arrays, you can also use key value pairs, so: Either way, I'd go for an SQL database. Much better to scale and to perform advanced searches.

Posted by LittleApps-Nick, 02-05-2013, 12:56 PM
Thanks everyone! I'm just going to use a MySQL database instead.

Posted by jainteq, 02-08-2013, 03:34 AM
I am beginner i am trying learn php, really this code is helpful for me, for every user has given information is good keep it up! Regards: Krishna Naidu.



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read
gigeservers (Views: 663)
DNS Prognation (Views: 616)

Language: