将数组元素移动到第一个位置但保持顺序

时间:2017-05-17 11:32:07

标签: php arrays

我不知道如何用英语正确地表达这个,因为这个问题看起来非常像this的重复,但我并不是说同样的事情。我想这也是为什么我找不到任何类似问题的原因,所以提前预测,因为这可能是重复的。

我想创建一个执行以下操作的函数:

功能重新排序($ arr,$ number)

 $arr = array('one','two','three','four','five')

 reorder($arr, 'two')  => 'two','three','four','five','one'
 reorder($arr, 'four') => 'four','five','one','two','three'
 reorder($arr, 'five') => 'five','one','two','three','four'

等...

谢谢!

4 个答案:

答案 0 :(得分:4)

根据您给出的示例,我假设您希望在您提供的值之前拆分数组,然后切换它们。应该这样做的函数是这样的:

function reorder($arr, $value) {
    $index = array_search($value, $arr);

    // Value does not exist in array
    if (false === $index) {
        return false;
    }

    $pre = array_splice($arr, $index);

    return array_merge($pre, $arr);
}

答案 1 :(得分:1)

这对你来说可能有点矫枉过正:

class myIterator implements Iterator {
    private $position = 0;
    private $array = array(
        'one',
        'two',
        'three',
        'four',
        'five'
    );  

    public function __construct() {
        $this->position = 0;
    }

    public function rewind() {
        $this->position = 0;
    }

    public function current() {
        return $this->array[$this->position];
    }

    public function key() {
        return $this->position;
    }

    public function next() {
        ++$this->position;
        if ($this->position >= count($this->array)) $this->rewind();
    }

    public function valid() {
        return isset($this->array[$this->position]);
    }

    public function reorder($desiredFirstElement) {
        foreach ($this->array as $ele) {
            if ($this->current() === $desiredFirstElement) return;

            $this->next();
        }
    }

    public function toArray() {
        $returnArray = [];
        foreach ($this->array as $ele) {
            $returnArray[] = $this->current();
            $this->next();
        }
        return $returnArray;
    }
}

$it = new myIterator;

$it->reorder('two'); //  => 'two','three','four','five','one'
var_dump($it->toArray());
$it->reorder('four'); // => 'four','five','one','two','three'
var_dump($it->toArray());
$it->reorder('five'); // => 'five','one','two','three','four'
var_dump($it->toArray());

答案 2 :(得分:0)

/* Cyclic Rotation - Rotate an array to the right by given number of steps.
 * $A = Array consisting N integers
 * $K = K times rotation
 * Returns Array with rotated values.
 */
function rotate($A,$K){
    if(is_array($A) && count($A)>0 && count($A) >= $K){
        for($i=0;$i<$K;$i++){
            /* $elm = array_pop($A);
            array_unshift($A, $elm); */
            $elm = array_shift($A);
            array_push($A,$elm);
        }
    }
    return $A;
}
/* Fech Key from of the element from array
 * $A = Array
 * $ele = string/value of array
 * Returns Array of rotated values.
 */
function fetchKey($A,$ele){
    $val = array();
    if(in_array($ele,$A)){
        $val = array_keys($A,$ele);
    }
    return rotate($A,$val[0]);
}
$arr = array('one','two','three','four','five');

$val = fetchKey($arr,'two');

var_export($val);

答案 3 :(得分:0)

看看这个。

$data = array('one','two','three','four','five');

function reorder($dataSet, $offsetValue)
{
    // find the index of the offsetValue
    $araayIndexPosition = array_search($offsetValue, $dataSet);

    $totalElements = count($dataSet);
    $newDataList = array();
    for ($i=$araayIndexPosition; $i <= $totalElements-1 ; $i++)
    {
        $newDataList[]=$dataSet[$i];
    }

    for ($i=0; $i <= $araayIndexPosition-1 ; $i++)
    {
        $newDataList[]=$dataSet[$i];
    }

    return $newDataList;
}

echo '<pre>';
print_r(reorder($data, 'four'));
echo '</pre>';