user3350885
user3350885

Reputation: 749

Handling Array values - unset or array_pop

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

Upvotes: 0

Views: 98

Answers (3)

nl-x
nl-x

Reputation: 11832

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

Upvotes: 0

Muhammad Zeeshan
Muhammad Zeeshan

Reputation: 8856

$my_array = $_POST;

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

unset($my_array['submit']);

Upvotes: 1

SajithNair
SajithNair

Reputation: 3867

<?php

unset($_POST['submit']);

?>

Upvotes: 2

Related Questions