Hello everyone!
Been working on an inventory system which stores all items in a string:
E.g:
Code: Select all
$inventory = "Sword,Potion,Sword,Dagger";
The thing is that I can manipulate the string by using explode and foreach for adding extra items to it but the problem comes when I want to delete an item, especially one that appears multiple times(like the Sword in the above example).
I tried str_replace,but it replaced all items of the same kind :
Code: Select all
$item = "Sword";
$string = str_replace("$item", "", $inventory);
My question is,how can I replace only one instance of a string that appears multiple times?