Forum Moderators: coopster
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!
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
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.