I want to unset an element in a global array. I have a delete button that refreshes the page and that deletes (or is supposed to ) whatever items in the array that were checked. This is what I have tried:

unset($GLOBALS['Array'][0]);

and this:

unset($_POST['Array'][0]);

when I try to print the value in Array[0] after using unset() it prints nothing, which is right. The problem is that where I am printing the entire array, it still holds the value of Array[0]. I think it is unsetting the local value but not the global. How do I unset the global value in the array?

    If you are unsetting a global variable inside a function you need to use:

    unset ($GLOBALS['VarName]']);
    

    If you have registar globals on then the post variables registered as globals are different from the $POST array. there fore unsetting the global post variable will not unset it in the $POST array and vice versa:

    unset ($GLOBALS['PostArray'][0]);
    
    /* prints original value with element [0] still set
        becuase $_POST is a different variable from $GLOBALS
      */
    
    var_dump ($_POST['PostArray']);
    
    /* however this will show the value as unset */
    
    var_dump ($GLOBALS['PostArray']);
    

      I am trying to delete a specific item in the array, if I do this:

      var_dump ($GLOBALS['PostArray']);

      I am not specifying an element of the array. I tried :

      var_dump ($GLOBALS['PostArray'][0]);

      but it doesn't work.

      Do you have any idea why?

      I am confused about the post and global variables being different. So let me see if I have this right... Even though I have one array that is global, there are actually two arrays, one that is the global array and one that is stored in the session????

      Thanks for your help!!

        Hi,

        Yes that is true. The $GLOBALS array holds every single global variable you have declared in your script. i.e any variable you use outside a function. The $POST and $GET are environment variables and are always set even it there are none ( in which
        case it would be an empt array).The $GLOBALS and $POST are two different variables. The reason why you can access your $POST and $GET variables through $GLOBALS is becuase you have a directive named "Register GLobals" turned on in your php.ini file. This directive causes a copy of all your $POST and $_GET variables to be added to the $GLOBALS array. This makes it more convenient when accessing the variables as they can be accessed by only their name:

        <?php
        /* The following HTTP request was made to this PHP script.
         * GET /test.php?var1=1&var2=2&var3=3 HTTP/1.0
         *
         * register globals is on so $var1, $var2, $var3 will be global 
         * variables to the script.
         */
        
        // only if register globals is on
        echo($var1); /* ouputs 1 */
        echo ($var2); /* outputs 2 */
        echo ($var3); /* outputs 3 */
        
        // all the time
        echo ($_GET['var1']); /* outputs 1 */
        echo ($_GET['var2']); /* outputs 2 */
        echo ($_GET['var3']); /* outputs 3 */
        ?>
        

        Lots of people don't use register globals as it potentially makes for in-secure scripts. Its a good idea if you are making a ditributed script to use the $_POST and $_GET environment variables as you can garuntee they will be there.

          11 days later

          I am still trying to figure out how to delete a specific element from a global array. I will explain my script, maybe that will give a better idea. First I am posting the values that will be stored in the array, then then I store them in a global array.

          Here is how I am setting the array

          //this is the function
          function InsertItem($Item){
          global $Array;
          if(!IsSet($_SESSION['Array'])){
          	$_SESSION['Array'][] = $Item;
          	$Array = $_SESSION[Array];
          	}
          
          else{
          	$Array = $_SESSION['Array'];
          	$Array[] = $Item;
          	$_SESSION['Array'] = $Array;
          }
          return $Array;
          }
          

          That is how I am getting values into my array. I was using just session variables, but I had to refresh the page every time a new value was added to the array in order to see it. I don't know if this helps any, but I have tried using the unset() function, which doesn't work with global values. I have also used var_dump, which has been suggested, but it doesn't work. Please help?!!!

            OK - Your function is very confusing. If you could explain the following:

            1. Why is the function's return value a global array $Array?

            2. For what particular reason are you modifying the $_SESSION superglobal?

            3. And I've never before seen this but it looks as if the function is a paradox.

              The $SESSION['Array'] is only modified if it is set in which case the entire array $SESSION['Array'] is copied to the global $Array

              If $SESSION['Array'] doesn't exist you copy the entire conents of the global $Array to $SESSION['Array']. You then proceed to add the item to the global $Array. You then copy the entire contents of the global $Array to $_SESSION['Array'].

              Why is this?

            As stated in one of the earlier posts to unset an element of a global array inside a function you use the following: unset($GLOBALS['MyArray']['MyElement']);.

              Here is what I am doing in the function:

              1. check if the array has been set
              2. if not, it stores the first value into the array
              3. if it has been set, then then just add to the array
              4. return the entire array

              It works, but is there another way you recomend doing this?

              I misunderstood what you were saying in the earlier post, so thanks for clearing that up for me. I will try using
              unset($GLOBALS['MyArray']['MyElement']);
              I'll post again and let you know how it works.
              Thanks for responding, I appreciate your help!

                I am not with it to day. Even though you are using session variables and I was writing about it I thought you were using $POST. So disregard what I said about changing the $SESSION super global.

                There may be a quicker and easier way to do it. However, I do not know in which context you are using the function and I don't want to change something that will break your script.

                You do not need to use return $Array; though because $Array is global. Return is only used to return a copy of the value of a local variable inside a function . Much the same as function parameters are used to copy the values of global variables for use in functions. If you have made a change to a global variable you do not need to return it.

                  Thanks for being so patient with me! I know it is taking me a while to catch on.

                  I changed my function to this:

                  function InsertItem($Item){
                  global $Array;
                  $_SESSION['Array'][] = $Item;
                  $Array = $_SESSION['Array'];
                  }
                  

                  I realized I didn't have to do all that work! And everything still works.

                  As for deleting things here is my script for that:

                  if(IsSet($delete_item) && is_array($delete_item)){ 
                  
                  $count = count($delete_item); 
                  echo "<br>You have selected " . $count. " items to delete!<br><br>\n"; 
                  
                  
                  for($j=0; $j<$count; $j++){
                  
                  $Arraylength = count($Array);
                  
                  for($z=0; $z<$Arraylength; $z++){
                  	$k = $j+1;
                  	if($Array[$z] == $k){
                  		print("The item to delete is $z is $Array[$z]");
                                              unset($GLOBALS['Array'][$z]);
                  		}
                                 }
                  }
                  
                  }
                  

                  It is working, except I have to refresh the page for it to show up. Is there a way to get around this? I have it where the page refreshes itself when the submit button is clicked but I still have to refresh it again to see that something has been deleted.
                  Thanks again for you help!!!! 🙂

                    5 days later

                    Sorry for the delay. in replying. I've been kinda busy.

                    All I can gather from your delete script is that the user is selecting some items to delete and submit them. PHP then loads them into an array.

                    I do not really understand what it is you are doing next though. If you could provide me with an explanation and maybe the HTML form you are using to submit the user choices I will see what I can do.

                      I am printing out what was selected to make sure it is working properly, then I use unset($GLOBALS['Array'][$z]); to remove the element from the array.
                      Here is my html form

                      <form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
                      
                      <?PHP
                      
                      //printing items in shopping cart and formatting table layout
                      for($i=0; $i<$ItemNumber; $i++){
                      
                      //checkbox is to delete an item
                      ?>	
                      <input type="checkbox" name="delete_item_array[]" value ="$i">" 
                      
                      <table border="1" bordercolor="#0099FF">
                      <tr><td>Product Name</td>
                      <td>Size</td>
                      <td>Price</td>
                      <td>Weight</td></tr>
                      <?PHP
                      printf("<tr><td> %s </td><td> %s </td><td> %s </td><td> %s  </td></tr>\n", $ProductName[$i],$Size[$i], $Price[$i], $Weight[$i]);
                      echo "</table>";
                      
                      }
                      echo "<input type=\"submit\" value=\"Delete Selected Items\">";
                      
                      if(IsSet($delete_item_array) && is_array($delete_item_array)){ 
                      
                      $count = count($delete_item_array); 
                      echo "<br>You have selected " . $count. " items to delete!<br><br>\n"; 
                      
                      
                      for($j=0; $j<$count; $j++){
                      print("<br>The item to delete is: $delete_item_array[$j]<br>");	
                      
                      
                      $size= count($ProductName);
                      
                      for($z=0; $z<$size; $z++){
                      $k = $j+1;
                      if($ProductName[$z] == $k){
                      	unset($GLOBALS['ProductName'][$z]);
                      
                      	}//end if
                      
                      
                      }//end for
                      
                      }//end for
                      echo "</form>";
                      

                      I hope that this makes sense. I just put what was relevant. I have since started making my html forms in an html file and keeping the php separate. But since I have to generate the checkbox and the tables I put it in with the php. Anyway, I hope I am not being confusing.

                        Write a Reply...