i have a foreach loop that goes through each element of an array and removes the element if needle isnt found in the value.
the problem is that the remove part doesnt seem to work, any ideas?

foreach ($temparr as $temparrval) {//loop through the array and remove the last entries until current page has been reached
   if (strpos($temparrval,$dir[0]))
      break;
   else
      array_shift($temparr);
}

does the foreach loop create a copy of the array when it loops through so you can not directly change it?
would i need to record the keys of the elements that are required to be removed and them remove them after the loop?

    a foreach loop is a shorthand way of writing
    while (list(, $value) = each($arr)) {
    so really the value assigned to $temparrval in your foreach, is just a copy of whats in the array. if you change that value it wont actually change whats in the array. it just provides an easy way to loop thru an array and have the abililty to get the keys if you dont know them, and step thru the values until you get thru all of them or get what you want.

    im not 100% sure if this is what you want to do but it may be close:

    while(strpos($temparr[0], $dir[0]) === FALSE && sizeof($temparr) > 0) {
      array_shift($temparr);
    }
    

    basically that will keep eliminating the first element while its not the dir you are looking for.

      hmm, since the foreach loop only supports references in version 5, would it be possible to create a copy array and then alter the copy of it? since it seems that when you directly edit the array thats being looped it seems to erase everything before and after what wants to be kept.

      example:

      [0] = 0
      [1] = 1
      [2] = 2

      if im checking for the value of 1, then i want the elements before (and not after) to be deleted

        ive changed the array slightly so what im looking for is the key to each element (the key is not used for anything else)

        $temparr2 = $temparr;
        foreach ($temparr as $temparrkey => $temparrval) {//loop through the array and remove the last entries until current page has been reached
           if ($temparrkey == $dir[0])
              break;
           else
              array_shift($temparr2);
        }
        

          yeah i would just build a new copy of the array

          also, you may be using strpos wrong.

          you need to check the result using either === or !== operators to explicitly check for true or false.

          for example

          
          if (strpos('a', 'a')) {
              echo 'found it';
          }
          
          

          doesnt work, because it returns position 0, and 0 evaluates to false.

          
          foreach ($orig_arr as $val) {
              if (false === strpos($val, $needle)) {
                  $new_arr[] = $value;
              }
          }
          
          

            ah thanks for that.

            does that go for empty() aswell?
            since in an if statement i have

            if (empty($this))

            returns false when $this = 0
            so would this work instead?

            if (FALSE === empty($this))

            oh and does the FALSE have to be before or can it be after the empty() part?

              You could also

              foreach($array as $key=>$value)
              {
                  if($value should be deleted)
                      unset($array[$key]);
              }

              This will also work with associative arrays where you want the keys to remain paired up with the appropriate values.

              However, for what you're doing, I'm wondering if [man]array_search[/man] and [man]array_slice[/man] might not provide a more straightforward (i.e., no looping) solution.

                empty() only returns true or false, so theres no need to do an explcit check

                strpos returns an integer which correspons to the position. since the position could be 0, and zero "evaluates" to false, thats why we need to check like that.

                btw

                
                if (empty($var)) {}
                
                // exact same as 
                
                if (!$var) {}
                
                
                

                the ONLY difference is php will not display a notice if the variable is not defined if your using empty.

                and yes, both integer and string zero evaluate to false. so does an empty array

                
                $var = 0;
                if ($var) {
                   echo 'yes'; // doesnt work
                }
                
                $var = '0';
                if ($var) {
                   echo 'yes'; // doesnt work
                }
                
                $var = array();
                if ($var) {
                   echo 'yes'; // doesnt work
                }
                
                
                // will work for all above examples
                if (false !== $var) {
                    echo 'yes'; // works
                }
                
                

                http://www.php.net/types
                http://www.php.net/manual/en/types.comparisons.php

                  you can also use isset() that's what I use to check and see if the variable is defined.

                    so could i use
                    false != $var
                    instead of empty($var)
                    since i do want to accept 0's

                      no.

                      if (false != $var)

                      is the same as

                      if ($var)
                      and
                      if (!empty($var))

                      you would do

                      $var != ''
                      or
                      strlen($var) > 0

                      there is a difference between == and ===
                      read those links i provided.

                      you should also do your own testing so you familiarize yourself with what does what.

                      and its generally a good practice to check if your variables even exist first. thats why you use isset() or empty() first

                      this would be a good practice

                      if (isset($var) && strlen($var)) {
                      // its set, and has a value
                      }

                        Write a Reply...