Jump to content

str_replace empty value question


avenged

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!
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.