【问题标题】:Handling Array values - unset or array_pop处理数组值 - unset 或 array_pop
【发布时间】:2014-04-22 07:55:54
【问题描述】:

我有这样的帖子价值。 它存储在$_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);
    ?>

谢谢,

金兹

【问题讨论】:

  • 你的问题不清楚..你能进一步解释一下吗?或者一些代码会有所帮助。
  • 再次检查我的问题。我已经粘贴了我的代码
  • 你也可以把意外的输出放在你的 OP 中吗?
  • 您不需要foreach 循环。 array_pop 应该只发生一次,因此不在循环中。更多here
  • unset($key['submit']); 是什么意思?我猜这会给你一个警告。 $key 是一个字符串,$key['submit'] 不存在...

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


【解决方案1】:

试试这样:

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

【讨论】:

    【解决方案2】:
    $my_array = $_POST;
    

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

    unset($my_array['submit']);

    【讨论】:

      【解决方案3】:
      <?php
      
      unset($_POST['submit']);
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 2014-04-28
        • 2011-05-03
        • 1970-01-01
        • 2015-10-01
        • 2013-12-25
        • 2015-01-19
        • 1970-01-01
        • 2018-07-16
        • 2012-12-22
        相关资源
        最近更新 更多