Jump to content

Archived

This topic is now archived and is closed to further replies.

avenged

str_replace empty value question

Recommended Posts

hello, was just wondering how I would be able to use str_replace to skip an index in an array if something is found in str_replace. I have a str_replace that checks an array for values in an array.

[code]
str_replace($array1, "", $array2);
[/code]

it searches array2 with values from array1, and i was wondering, if it finds a match, instead of it replacing it with a space, how do i replace it with nothing, as in the array returned wont even have an index with anything in it with that?. say i am searching an array with values:

array('1', '2', '3');

and i want to search it from the values contained in this array:
array('1');
so, obviously, it would return a match with this array:
array('', '2', '3');

How would I replace the match with nothing, so it would return an array like this:
array('2', '3');

??

Thanks!

Share this post


Link to post
Share on other sites
One clear cut solution would be.

Get the size of the array, Loop through the aray and replace each instance in the array.

[code]

for ($i=0;$i<sizeof($array1);$i++)
{
  if ($array1[$i] == "What ever")       #strcmp could also be used
    {
       $array1[$i] = "";    #Use any value
     }
}
[/code]

You can use your own logic to read the array & replace the occurences found within.

Hope this will puts some light.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.