rockinaway Posted February 17, 2008 Share Posted February 17, 2008 I have some values e.g. 11112234444 and an array - $array I want to do a check to see if they are present in $array, if they aren't then I want to add them in. Then later I want to see if they are present in that $array (there should only be 1 occurrence of each value), and if they are, I want to echo out 'Yes' and REMOVE the occurrence from $array... So for example: 1 is tested... it isn't in the array, so it is added in.. the others 1s are NOT added in, as there is already 1 in $array Later I want to check for 1 in the $array again, and this time I want to remove it from the array, so that there isn't 1 left in the $array.... Any help on what I can do? Sorry hard to explain :S Quote Link to comment Share on other sites More sharing options...
Chris92 Posted February 17, 2008 Share Posted February 17, 2008 Lucky for you I just made the same sort of thing. Study this: $array = array("1","1","1","1","2","2","3","3","4","4"); for($i=0; $i <= count($array); $i++) { if( !in_array("{$array[$i]}", $array2) ) { $array2[] = $array[$i]; } } foreach( $array2 as $value ) { echo $value; //should return 1234 } Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted February 17, 2008 Share Posted February 17, 2008 <?php $array = array(); // ... if(!in_array($value, $array)) $array[] = $value; // ... $search = array_search($value, $array); if($search !== false) { unset($array[$search]); } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.