Jump to content

Problems with a flag


Armada

Recommended Posts

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

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.