0

I have a post value like this. It is stored in $_POST. Now, I need to remove or unset the last array value i.e [submit] => Add. When I checked in SOF, they asked me to use array_pop. But that didn't work. Any help.

My output:

    [56-1] => 0
    [56-2] => 0
    [56-3] => 0
    [submit] => Add

Expected output:

        [56-1] => 0
        [56-2] => 0
        [56-3] => 0

EDITED:

Here is my code

    <?php 
    $my_array = $_POST;
    foreach($my_array as $key=>$value){
    array_pop($my_array);
    unset($key['submit']);
    }
    print_r($my_array);
    ?>

Thanks,

Kimz

user3350885
  • 739
  • 4
  • 16
  • 38

3 Answers3

2
<?php

unset($_POST['submit']);

?>
SajithNair
  • 3,867
  • 1
  • 17
  • 23
1
$my_array = $_POST;

Don't need to use loop. You should do:

unset($my_array['submit']);

Muhammad Zeeshan
  • 8,722
  • 10
  • 45
  • 55
0

Try it like this:

<?php 
    $my_array = $_POST;
    foreach($my_array as $key=>$value){ // this foreach will eventually pop ALL your items
        echo "foreach is now at value: $value<br/>"; // remove this line if you remove the foreach
        echo "but now removing the last value of array (pop), which is value: "
        echo array_pop($my_array) . "<br/>";
        echo "done<br/>";
        // unset($key['submit']); // remove this line. this is foobar. $key['submit'] will never exist.
    }
    print_r($my_array); // with the foreach, this will print nothing (empty). without the foreach, only the last line would print...
?>
nl-x
  • 11,762
  • 7
  • 33
  • 61