这个数组函数是否存在于PHP中?

时间:2010-08-19 21:39:52

标签: php arrays

是否有PHP函数可以执行以下操作:

从其当前索引移动指定的数组值,然后执行以下操作:

  1. 在两个现有指数之间移动。
  2. 使用第二个索引/值进行交换。
  3. 我在下面的课程中完成了这项任务,但是想知道PHP库中是否存在预先存在的函数?

    这是我为该任务创建的类:

    class Rearranger
    {   
        /*
         * Determines how to move the specified value.
         */
        public static function go($list, $indexToMove, $offset)
        {
            if(($offset == ($indexToMove - 1)) || ($offset == ($indexToMove + 1))) {
                //Value is only being moved up or down one index, so just swap the values
                $list = self::swap($list, $indexToMove, $offset);
            } else if($offset < $indexToMove) {
                //Value is being moved up (will be changing to a lower value index)
                $list = self::move($list, $indexToMove, $offset);
            } else if($offset > $indexToMove) {
                //Value will be moving down (will be changing to a higher value index)
                $list = self::move($list, $indexToMove, $offset - 1);
            } 
            return $list;
        }
    
        /* 
         * Moves the value at $list[$indexToMove] in between
         * $list[$offset - 1] and $list[$offset].
         */
        public static function move($list, $indexToMove, $offset)
        {
            $container = $list[$indexToMove];
            unset($list[$indexToMove]);
            $list = array_values($list);
            $list2 = array_slice($list, $offset);
            $list = array_slice($list, 0, $offset);
            array_push($list, $container);
            return array_merge($list, $list2);
        }
    
        //Swap the values of two array indices
        public static function swap($list, $indexA, $indexB)
        {
            $vA = $list[$indexA];
            $vB = $list[$indexB];
            $list[$indexA] = $vB;
            $list[$indexB] = $vA;
            return $list;
        }
    }
    

    以下是Rearranger类的一些示例用法:

    $a1 = array('a', 'b', 'c', 'd', 'e', 'f');
    
    function display($list) { echo '<p>' . var_dump($list) . '</p>'; }
    
    //Move value at index 4 to between index 0 and 1
    $a1 = Rearranger::go($a1, 4, 1);
    display($a1);
    
    //Move value at index 1 to between index 3 and 4
    $a1 = Rearranger::go($a1, 1, 4);
    display($a1);
    
    //Swap value 2 and 3    
    $a1 = Rearranger::go($a1, 2, 3);
    display($a1);
    
    //Swap value 5 and 4 
    $a1 = Rearranger::go($a1, 5, 4);
    display($a1);
    
    //Do nothing
    $a1 = Rearranger::go($a1, 2, 2);
    display($a1);
    

2 个答案:

答案 0 :(得分:2)

你的意思是排序算法吗?那么有很多函数可以在php中对数组进行排序。您需要的功能取决于您想要排序的巫婆的确切要求。

多年来已经设计了很多类型的alghoritms,需要look

修改 怎么样:

$source = array('a','b','c','d');
$head = array_splice($source,0,2);
$tail = array_splice($source,2,4);
$new = array_merge($head,array('e'),$tail);
// should yield array('a','b','e','c','d')
// Please check the array_splice documentation, iam not a 100% sure on the syntax.

这样你就可以在数组中插入一个新元素。应该在你的路上帮助你。

答案 1 :(得分:2)

$a1 = array('a', 'b', 'c', 'd', 'e', 'f');

在索引处插入数组元素:

array_splice($array, 3, 0, 'Z'); 
print_r($a1);
  

阵列(       [0] =&gt;一个       [1] =&gt; b       [2] =&gt; C       [3] =&gt; ž       [4] =&gt; d       [5] =&gt; Ë       [6] =&gt; f)

交换数组元素:

list($a1[4], $a1[3]) = array($a1[3], $a1[4]);
print_r($a1);
  

阵列(       [0] =&gt;一个       [1] =&gt; b       [2] =&gt; C       [3] =&gt; d       [4] =&gt; ž       [5] =&gt; Ë       [6] =&gt; f)