Armada Posted June 7, 2008 Share Posted June 7, 2008 Hey everybody, this is a pretty simple problem, but I just can't see how to fix it. Here's my code block: <?php //Show what item was used and what happens #There are currently 3 item values ($item_val[$position] == 1){ echo "You break your rock into 2 pebbles. How exciting!"; //Rebuild the string removing the item #Get the upperboundary for use in a foreach loop foreach($item_val as $value){ $i++; } //Here's the start of the problem $f1 == false; #Now rebuild the string from the array for ($j=0; $j <= $i-1; $j++ ){ #Eliminate the used item ($f1 is the flag that stops eliminating duplicates) if(($item_val[$j] == 1 && $f1 == true) || ($item_val[$j] != 1)){ $write_val .= $item_val[$j]; $f1 == true; } } } ?> I commented where my problem starts. Now, I have an array that I'm rebuilding into a string. That works frine. It's value is 1,3,2,1,1. Now, I just want to strip out the first "1" it finds, and leave the others, so it's 3,2,1,1. My output is still 1,3,2,1,1. Does anyone see my obvious error? EDIT: If I use "if(($item_val[$j] == 1 && $f1 == true) || ($item_val[$j] != 1))" it outputs 3,2 Quote Link to comment Share on other sites More sharing options...
.josh Posted June 7, 2008 Share Posted June 7, 2008 instead of using all those conditions and loops, you can use preg_match to find the first occurance and deal with it accordingly. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 7, 2008 Share Posted June 7, 2008 Well, there is a lot of unneccessary code. For example: foreach($item_val as $value){ $i++; } You could get the same thing by $i = count($item_val). But even that is unnecessary. <?php //Remove the first index w/ the value of 1 (if it exists) if (array_search(1, $item_val)!==false) { unset($item_val[array_search(1, $item_val)]); } //Combine remaining array elements into a string $write_val = implode('', $item_val); ?> The IF statment may not even be necessary, but I didn't take the time to test it. Quote Link to comment Share on other sites More sharing options...
Armada Posted June 7, 2008 Author Share Posted June 7, 2008 Well, there is a lot of unneccessary code. For example: foreach($item_val as $value){ $i++; } You could get the same thing by $i = count($item_val). But even that is unnecessary. <?php //Remove the first index w/ the value of 1 (if it exists) if (array_search(1, $item_val)!==false) { unset($item_val[array_search(1, $item_val)]); } //Combine remaining array elements into a string $write_val = implode('', $item_val); ?> The IF statment may not even be necessary, but I didn't take the time to test it. Thanks! That worked perfectly. I knew I had a lot of unnecessary code, I just wanted to debug this sucker after working on it for 2 days. Anyway, thanks again. 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.