【问题标题】:PHP | Remove element from array with reordering?PHP |通过重新排序从数组中删除元素?
【发布时间】:2011-01-11 11:46:27
【问题描述】:

如何删除数组中的元素,然后重新排序,而数组中没有空元素?

<?php
   $c = array( 0=>12,1=>32 );
   unset($c[0]); // will distort the array.
?>

答案/解决方案:array array_values (array $input)。

<?php
   $c = array( 0=>12,1=>32 );
   unset($c[0]);
   print_r(array_values($c));
   // will print: the array cleared
?>

【问题讨论】:

    标签: php arrays pointers return unset


    【解决方案1】:
    array_values($c)
    

    将返回一个仅包含线性索引的值的新数组。

    【讨论】:

      【解决方案2】:

      如果您总是删除第一个元素,请使用 array_shift() 而不是 unset()。

      否则,您应该能够使用 $a = array_values($a) 之类的东西。

      【讨论】:

        【解决方案3】:

        另一个选项是array_splice()。如果您要处理足够多的数据来处理,这会重新排序数字键,并且似乎是一种更快的方法。但我喜欢 unset() array_values() 的可读性。

        array_splice( $array, $index, $num_elements_to_remove);
        

        http://php.net/manual/en/function.array-splice.php

        速度测试:

            ArraySplice process used 7468 ms for its computations
            ArraySplice spent 918 ms in system calls
            UnsetReorder process used 9963 ms for its computations
            UnsetReorder spent 31 ms in system calls
        

        测试代码:

        function rutime($ru, $rus, $index) {
            return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000))
             -  ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000));
        }
        
        function time_output($title, $rustart, $ru) {
                echo $title . " process used " . rutime($ru, $rustart, "utime") .
                    " ms for its computations\n";
                echo $title . " spent " . rutime($ru, $rustart, "stime") .
                    " ms in system calls\n";
        }
        
        $test = array();
        for($i = 0; $i<100000; $i++){
                $test[$i] = $i;
        }
        
        $rustart = getrusage();
        for ($i = 0; $i<1000; $i++){
                array_splice($test,90000,1);
        }
        $ru = getrusage();
        time_output('ArraySplice', $rustart, $ru);
        
        unset($test);
        $test = array();
        for($i = 0; $i<100000; $i++){
                $test[$i] = $i;
        }
        
        $rustart = getrusage();
        for ($i = 0; $i<1000; $i++){
                unset($test[90000]);
                $test = array_values($test);
        }
        $ru = getrusage();
        time_output('UnsetReorder', $rustart, $ru);
        

        【讨论】:

          【解决方案4】:

          如果你只删除数组的第一项,你可以使用array_shift($c);

          【讨论】:

            【解决方案5】:

            array_shift() 将数组的第一个值移开并返回,将数组缩短一个元素并将所有内容向下移动。所有数字数组键都将被修改为从零开始计数,而文字键不会被触摸。

            array_shift($stack);

            示例:

            $stack = array("orange", "banana", "apple", "raspberry");
            $fruit = array_shift($stack);
            print_r($stack);
            

            输出:

            Array
            (
                [0] => banana
                [1] => apple
                [2] => raspberry
            )
            

            来源:http://php.net/manual/en/function.array-shift.php

            【讨论】:

            【解决方案6】:
            $array=["one"=>1,"two"=>2,"three"=>3];
            $newArray=array_shift($array);
            
            return array_values($newArray);
            

            返回 [2,3] array_shift 从数组中删除第一个元素 array_values 只返回值

            【讨论】:

              【解决方案7】:

              或者reset();也是不错的选择

              【讨论】:

              • reset(); 不是,根据 PHP.net:“reset() 将数组的内部指针倒回第一个元素并返回第一个数组元素的值。”
              猜你喜欢
              • 2021-10-01
              • 2012-09-17
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-08-29
              • 1970-01-01
              • 2021-01-20
              相关资源
              最近更新 更多