Jump to content

removing part of the array


MrAdam

Recommended Posts

hey all!i've been on with this for past half hour, and im a bit frustrated now!i have a variable which contains an id. In some cases the id may be repeated (multiple times), and a comma inbetween the id's. for example: "33" OR "33,33" OR "33,33,33" etc.im trying to create a simple function to remove just one of the id's: so, "33,33" becomes "33".my question is; how do I do it ?! :) -Thanks!

Link to comment
Share on other sites

Explode the variable into an array and then use the array_unique () to reduce it down to a single occurence.An alternate would be to explode the variable into an array and use the array(0) element.

Link to comment
Share on other sites

i managed to sort it:

$id = "72"; // usually i would have run some statements to be left with the array[x] matching my id$test = Array();$test[0] = "72,72,72";$len = strlen($id) + 1; // length of ID plus 1 more for the comma$rem = strlen($test[0]) - $len; // the remainder from length of the array[x] minus $len$test[0] = substr($test[0],$len,$rem); // use substr() to only select after the first id, effectivelly, only removing one of the IDs

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...