【问题标题】:How to remove the last element from array without using array_pop如何在不使用array_pop的情况下从数组中删除最后一个元素
【发布时间】:2018-09-19 16:55:51
【问题描述】:

我有一个字符串

 $Community = "1,2,3,4,";
 $ExplodeCommunity = explode(',',$Community);//Split 
 print_r($ExplodeCommunity);

这给了

  Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => ) 

现在我想删除数组中的最后一个元素。我试过 array_pop

  $RemovedLaste = array_pop($ExplodeCommunity);

  print_r($RemovedLaste);

但是什么都没有打印出来。如何在不使用array_pop的情况下从数组中删除最后一个元素

【问题讨论】:

  • 为什么不直接使用$Community = rtrim($Community, ','); 而不是额外的数组函数。
  • 您的代码工作正常,您正在尝试打印一个空元素。打印原始数组以确认它有效:print_r($ExplodeCommunity);
  • 在这种情况下array_pop() 删除最后一个元素
  • 当您执行print_r($RemovedLaste); 时,您希望看到什么打印?你弹出了一个空字符串,所以 print_r() 将显示一个空字符串
  • 你的最后一个元素是空的

标签: php arrays


【解决方案1】:

array_pop 使用引用,因此会影响原始数组...试试这个...

$x=array_pop($ExplodeCommunity);
print_r($ExplodeCommunity);
var_dump($x);

看看你得到了什么,顺便说一句你想打印空值?

【讨论】:

  • 它打印没有最后一个元素的数组,但我们需要打印最后一个元素
  • @ArtemIlchenko 这就是标题所暗示的 op 想要的
  • ExplodeCommunity 中的最后一个条目是空的,所以没有可打印的内容?
  • OP严格要求last element from array without using array_pop
【解决方案2】:
   function remove_last($Community) {

            $ExplodeCommunity = explode(',',$Community);
            $remove_empty = array_values(array_filter($ExplodeCommunity));
            $last_value_in_array =  end($remove_empty); 
            $lengh = sizeof($remove_empty) -1 ;
            unset($remove_empty[$lengh]);   
            return $remove_empty; 
    }

    $Community = "1,2,3,4,";
    print_r(remove_last($Community)); 

    Output : Array ( [0] => 1 [1] => 2 [2] => 3 )

【讨论】:

  • OP严格要求last element from array without using array_pop
【解决方案3】:

发现问题

当您使用下面提供的代码explode 时,您遇到的问题是字符串'1,2,3,4,' 中的最后一个逗号,:

$Community = "1,2,3,4,";
                   //^ The value after this is an empty string ''
$ExplodeCommunity = explode(',',$Community); 

这导致$ExplodeCommunity 本质上是这样的,其中最后一个条目是一个空字符串:

$ExplodeCommunity = [1,2,3,4,''] 

这就是为什么你的Array 中有 5 个条目的原因

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => ) 
                                                  ^ This is an empty string

函数array_pop 返回Array 中最后一个条目的值,在本例中为空字符串''。

$RemovedLaste = array_pop($ExplodeCommunity);
print_r($RemovedLaste); // This prints an empty string ''

$ExplodeCommunity 的值为:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4) 


解决方案 1:保持 $Community 值不变:

如果您在$ExplodeCommunity 上执行两次array_pop,您将获得 4:

$Community = "1,2,3,4,";
                   //^ The value after this is an empty string ''
$ExplodeCommunity = explode(',',$Community); 

$RemovedLaste = array_pop($ExplodeCommunity);
print_r($RemovedLaste); // This prints an empty string ''

$RemovedLaste = array_pop($ExplodeCommunity);
print_r($RemovedLaste); // This prints 4

$ExplodeCommunity 的值为:

Array ( [0] => 1 [1] => 2 [2] => 3) 


解决方案2:使用rtrim去掉结尾逗号,:

您可以使用rtrim 如下所示从$Community 字符串中删除结尾逗号:

$Community = rtrim("1,2,3,4,", ',');
$ExplodeCommunity = explode(',',$Community); 

$RemovedLaste = array_pop($ExplodeCommunity);
print_r($RemovedLaste); // This prints 4

$ExplodeCommunity 的值为:

Array ( [0] => 1 [1] => 2 [2] => 3) 

其他解决方案

您可以按照其他答案中的说明使用其他功能,例如unset/array_slice。

【讨论】:

    【解决方案4】:

    您可以使用unset

    unset($ExplodeCommunity[count($ExplodeCommunity) - 1] )
    

    【讨论】:

      【解决方案5】:

      试试这个...

      if(empty($ExplodeCommunity[count($ExplodeCommunity)-1])) {
          unset($ExplodeCommunity[count($ExplodeCommunity)-1]);
      }
      

      【讨论】:

      • 感谢您提供此代码 sn-p,它可能会提供一些即时帮助。正确解释would greatly improve 其教育价值,通过说明为什么这是解决问题的好方法,并使其对未来有类似但不相同问题的读者更有用。请编辑您的答案以添加解释,并说明适用的限制和假设。
      【解决方案6】:

      试试这个:

      $arr = array('hello', 'world', '2');
      $arr = unset(end($arr));
      

      这将“取消设置”数组的“结束”元素。

      【讨论】:

        【解决方案7】:

        你也可以试试array_slice

        array_slice :返回数组数组中的元素序列 由偏移量和长度参数指定。

        $RemovedLaste = array_slice(ExplodeCommunity, 0,-1);
        print_r($RemovedLaste);
        

        【讨论】:

          【解决方案8】:

          从 php 4 开始,您可以像这样使用explode 函数删除最后一个元素:

          $Community = "1,2,3,4,";
          $ExplodeCommunity = explode(',', $Community,-1);
          print_r($ExplodeCommunity);
          

          这会给你:

          Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4)
          

          【讨论】:

            猜你喜欢
            • 2021-09-08
            • 2014-11-10
            • 2019-10-05
            • 2014-12-09
            • 2021-12-28
            • 1970-01-01
            • 2012-01-05
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多