处理数组值 - unset或array_pop

时间:2014-03-17 07:24:42

标签: php arrays arraylist multidimensional-array array-population

我有这样的帖子值。 它存储在$_POST中。 现在,我需要删除或取消设置最后一个数组值,即[submit] => Add。 当我签入SOF时,他们让我使用array_pop。 但那并没有奏效。任何帮助。

我的输出:

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

预期输出:

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

编辑:

这是我的代码

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

谢谢,

Kimz

3 个答案:

答案 0 :(得分:2)

<?php

unset($_POST['submit']);

?>

答案 1 :(得分:1)

$my_array = $_POST;

不需要使用循环。 你应该这样做:

unset($my_array['submit']);

答案 2 :(得分:0)

试试这样:

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