Hello there,

so what do i want to do??

k... i got an array...

say with 5 variables... <-- 'someArray[0-4]'

now.. i want to delete a part of it... say 'someArray[2]'...

and WHEN it is deleted.. i want... the array to be resized to 'someArray[0-3]'

hope you understand... AND can help me...

tnx for reading anyway...

    I know i can code myself AROUND the problem...

    how?

    simple..
    remember my simple 'someArray'-example??
    ...I could set the value of the one i want deleted to null...

    like so...

    someArray[2] = ""

    and then... create a new array... wich will not put in the array-value when there's nothing in it... like so...

    $i =0;
    WHILE ($i <> Ubound(someArray) {
    if (someArray[$i] <> "") {
    newArray[$i] = someArray[$i]; }
    $i++; }

    think i might just do that... tnx anyway... <-- for READING... tnx... :rolleyes: :rolleyes:

    Edit:
    Then why open a NEW topic for it??
    I dunno... didnt occur to me... that i could solve this one myself... sorry...:o 😃

      I thought there was a built in function for this but a cant seem to find it so the following code should work.

      <?
      Function Remove($arrName, $arrIndex) {
          $ubound = count($arrName) - 1;
          if ($arrIndex > $ubound) {
              return False;
          }else{
              $arrName[$arrIndex] = "";
              if ($arrIndex != $ubound) {
                  for ($a=$arrIndex + 1;$a<=$ubound;$a++) {
                      $arrName[$a - 1] = $arrName[$a]
                  }
              }
          }
          return $arrName;
      }
      ?>

        Or ...

        function array_remove_item($array, $index){
          for(var $i = 0; $i < count($array); $i++)
            if($i != $index)
              $return_array[] = $array[$i];
          return $return_array;
        }
        

          wow...

          thanks!!!

          great stuff...

          esp. the remove_item-function...i'll use it... tnx!!!😃 😃

            Probably could improve to:

            function array_remove_item($array, $index) {
            	foreach ($array as $key => $value)
            		if ($key != $index)
            			$return_array[] = $value;
            	return $return_array;
            }

            or at least take the count() out of the loop check to improve efficiency.

            Actually, if it is feasible, you could unset() the array element then loop through using foreach() instead of a for loop.
            That might be even faster, but again, only if it is feasible.

              tnx for the tips.. all..

              but this is what i do...

              $number = 2;
              $info = array(1,2,3,4,5);

              $info2 = array()
              $info2 = array_merge(array_splice($info, 0, $number-1), array_splice($info, 1));

              Why not use "array_remove_item"??

              'cause i cant find it anywhere on the internet... (searched Google and some php-code-sites)...

              but... tnx again...

                I'm upgrading my array_remove_item() function to Laserlight's.

                  allrightiethen...

                  ones again... tnx all...

                    Actually, if you want to use array_splice() you should use:

                    $info2 = array_splice($info, $number, count($info) - $number, array_splice($info, $number + 1));

                    This may or may not improve on efficiency compared to an efficient version of Paulnaj's code.

                      Originally posted by laserlight
                      Actually, if you want to use array_splice() you should use:

                      $info2 = array_splice($info, $number, array_splice($info, $number + 1));

                      This may or may not improve on efficiency compared to an efficient version of Paulnaj's code. [/B]

                      hmm... nice...

                      but you forgot the array_MURGE.... (i think)... right?

                      or was that on purpose??

                        array_splice() is intended for replacement of part an array, so in this case you're replacing part of the array with part of this part.

                        Actually, my implementation is still wrong, since now $info contains what you want.

                        The earlier algorithm is to loop through the array and discard the element that is to be removed.
                        In this way, the elements below are assigned new keys.

                          ok, I did a test:

                          array_splice($info, $num, count($info) - $num, array_splice($info, $num + 1));

                          should do the same thing as

                          $info = array_remove_item($array, $num);

                            If course, if you're only removing one element at a time and you want to reindex afterwards, you could just go

                            unset($info[$number]);
                            $info = array_values($info);
                            

                              THANK YOU!!!

                              THANK YOU!!!

                              that code is just SUPERBE!!!

                              tnx mate...

                                Write a Reply...