2010-08-19 66 views
1

是否有一个PHP函数,将做到以下几点:这个数组函数是否存在于PHP中?

移动指定数组值从它的当前索引,然后或者:

  1. 感动在现有的两个指标之间。
  2. 与第二个索引/值交换。

我为该任务创建了下面的类,但是想知道PHP库中是否存在预先存在的函数?

这里是I类已为任务创建:

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; 
    } 
} 

以下是重新排列器类的一些示例用法:

$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

重排脏的声音对我) – Gordon 2010-08-19 22:03:35

+0

啊,但脏是在你的​​脑海:-) – 2010-08-19 22:04:51

回答

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. 

这样,你应该能够插入一个新元素到数组。应该帮助你。在索引

+0

嗯,我提供的具体要求。 – 2010-08-19 21:51:40

+0

感谢您的链接,虽然 – 2010-08-19 21:57:09

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

插入数组元素:

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

阵列( [0] =>一个 [1] => b [2] => C^ [3] = >ž [4] => d [5] =>电子 [6] => F)

交换数组元素:

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

阵列( [0] =>一个 [1] => b [2] => C^ [3] => d [4] =>ž [5] =>电子 [6] => F)

+0

我也尝试使用array_splice,但它并没有解决采取一个值并插入两个其他值(不覆盖任何东西)之间的问题。例如,取$ a1 [3]并将其值插入$ a1 [4]和$ a1 [5]之间。感谢您的答复 – 2010-08-19 22:17:38

相关问题