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
https://forums.phpfreaks.com/topic/109160-problems-with-a-flag/
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
https://forums.phpfreaks.com/topic/109160-problems-with-a-flag/#findComment-559940
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
https://forums.phpfreaks.com/topic/109160-problems-with-a-flag/#findComment-559945
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.