Forum Moderators: coopster

Message Too Old, No Replies

Searching An Array Against An Array

         

trigoon

1:05 pm on Jun 14, 2008 (gmt 0)

10+ Year Member



Hello,

I'm trying to accomplish this but for some reason I just cant figure out what I'm doing wrong.

I have a search function I'm working on. Now for my website the search function searches for something tagged with the exact tag that is being searched for.

I have it setup so that when you make a search the tags you search for are set into an array.

I run a function on that array before performing the search. Now what the function does is that it takes the array and does the following:

// Foreach keyword..
foreach ($array as $key => $value) {
// Take the value, if it is one of the keys in the fixarray, replace it
if (array_key_exists($value, $fixarray)) {
$array[$key] = $fixarray[$value];
}
}

The $fixarray you see is one located in the function itself which is setup like this:

$fixarray = $array(
"problematic query" => "replaced query",
"problematic query" => "replaced query",
"problematic query" => "replaced query",
"problematic query" => "replaced query"
);

For some reason though I just cant get the above to work, if someone would be kind enough to explain why it doesn't work and/or provide me with an alternate option I would be grateful!

Thanks!

dreamcatcher

8:20 am on Jun 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi trigoon,

I`m a little unclear what you are trying to do. Can you post your function? Are you trying a find and replace? You can use str_replace [uk2.php.net] for that as it supports arrays.

$fixarray = $array(
"problematic query",
"problematic query",
"problematic query",
"problematic query"
);

$fixarray2 = $array(
"replaced query",
"replaced query",
"replaced query",
"replaced query"
);

$string = str_replace($fixarray1,$fixarray2,$string);

dc

trigoon

11:48 am on Jun 15, 2008 (gmt 0)

10+ Year Member



Hello,

Thanks for the reply that might work but the string in that case is an array. I tried in two ways, working with it as an array and working with it as a string. Below is the function simplified a bit (solution and problem array wise:

function clean_tag_array($array) {

$problem= array(
"colour",
"coloured",
"colors",
"coloring"
);

$solution= array (
"color",
"color",
"color",
"color"
);

// Converted to a string
$array2= implode(",", $array);
$array2 = str_ireplace($problem,$solution,$array2);
$array= explode(",", $array2);

OR <---

// Working with it as an array
// Foreach keyword..
foreach ($array as $key => $value) {
$value2 = str_replace($problem,$solution,$value);
$array[$key] = $value2;
}

return $array;

}

Edit: Forgot to mention the above just isnt working for me.

trigoon

3:58 pm on Jun 15, 2008 (gmt 0)

10+ Year Member



Fixed the problem. The above does work it was just the way the array being inputted into the function was formatted. Thanks for the help.

dreamcatcher

6:54 pm on Jun 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you got it sorted.

dc