Reputation: 749
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
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
Reputation: 8856
$my_array = $_POST;
Don't need to use loop. You should do:
unset($my_array['submit'])
;
Upvotes: 1