Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching and sorting array by keyword and displaying results

Status
Not open for further replies.

spastica

Programmer
Sep 27, 2002
72
GB
I am creating an image gallery which puts all images into an array, and then sorts them accordign to variables specified in the querystring. For example, only images belonging to a certain gallery name will show if the gallery name is defined in the querystring.

I want search for a keyword in the existing array, then have the results returned in a new array, which I will use to display the images in order.

I am having problems trying to search and sort the array by keyword.

Here is a sample of how my array is set up:

$img[1] = Array("imgName","url_of_image","GalleryName","Other_keywords_to_sortBy");
$img[2] = Array("Cat","/images/photos/cat.jpg","BlackCats","cats");
$img[3] = Array("Cat2","/images/photos/cat2.jpg","BlackCats","cats");
$img[4] = Array("Cat3","/images/photos/ca3t.jpg","StripedCats","stripes");

So, if the querystring specifies the sortType as gallery and subCategory as cats (both specified in querystring), I would like to search the galleryname of the entire array to see if it matches the subCategory (in this case, cats) and then display only those results on the page.

Not sure if this is makign sense, but I am relatively new to arrays of this sort in php, and would like any advice/suggestions/help.
 
you might consider restructuring your arrays to make their use easier. e.g.
Code:
$img[1] = Array(
           "imgname"=>"imgName",
           "url_of_image"=>"url_of_image",
           "GalleryName"=>"GalleryName",
           "other keywords"=>"Other_keywords_to_sortBy");

but you can do what you want through something like the following:

Code:
foreach ($img as $array):
  if (in_array("searchterm", $array)):
   $newarray[] = $array;
  endif;
endforeach;
 
thank you! that is searching through the array and creating a new one with the search matches perfectly :)

i need to narrow it down a bit more though:

is it possible to search only a specific key in the array, instead of all the keys? For example, if the user chooses to sort by gallery, i would like to search only values under the key corresponding with galleryname (in this case, [2]) and if the user chooses to sort by subject, i would like to only search through the keyword list (in this case [3]).

Another question - i want the keyword list to contain more than 1 word - can i keep it all as one key and put in a list of words seperated by semicolons, but have the search recognize each semi-colon seperated word as seperate when looking for a match? currently it is parsing as one long string.
 
store the key words as an array.
Code:
$keywords = array("cat", "dog", "bunny");

for a specific search it's as simple as
Code:
foreach ($img as $array):
  if (in_array($array[2], $keywords)): //change this to [3] for the other search
   $newarray[] = $array;
  endif;
endforeach;
note that the above is case sensitive. if you want a case insensitive verstion then you would have to do the following:
1. make sure all you keywords were in lower case
2. change the relevant line of the code to
Code:
if (in_array(strtolower($array[2]), $keywords)):

note also that the in_array only works when the particular key you are searching is not itself an array.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top