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

6
  • your question's unclear..can you explain it further or some code will help. Commented Mar 17, 2014 at 7:26
  • check my question again. i have pasted my code Commented Mar 17, 2014 at 7:29
  • can you also put the unexpected output in your OP ? Commented Mar 17, 2014 at 7:30
  • You do not need a foreach loop. The array_pop should happen only once and thus out of the loop. More here Commented Mar 17, 2014 at 7:32
  • what is unset($key['submit']); meant to do? I would guess this would give you a warning. $key is a string, and $key['submit'] doesn't exist... Commented Mar 17, 2014 at 7:32

3 Answers 3

2
<?php

unset($_POST['submit']);

?>
Sign up to request clarification or add additional context in comments.

Comments

1
$my_array = $_POST;

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

unset($my_array['submit']);

Comments

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...
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.