-1

I don't know how to express this in english properly, because this question looks very much like a duplcate of this, still I don't mean the same thing. I guess that is also the reason why I can't find any similar question, so appologies in advance as this could be a duplicate.

I want to create a function that does the following:

function reorder($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'

etc...

thanks!

Community
  • 1
  • 1
Roddeh
  • 207
  • 1
  • 6
  • 19
  • Possible duplicate of [Move array item with certain key to the first position in an array, PHP](http://stackoverflow.com/questions/11703729/move-array-item-with-certain-key-to-the-first-position-in-an-array-php) – schellingerht May 17 '17 at 11:41
  • Hey @Roddeh! I have an easy solution, [look into it!](http://stackoverflow.com/a/44024091/2679536) – Shaunak Shukla May 17 '17 at 12:05
  • Well, looking at the answer I guess this is a not a duplicate, feel free to alter my question so that it fits the solution. – Roddeh May 17 '17 at 12:23

4 Answers4

4

Based on the examples you give I assume you want to split the array before the value you provide and then switch them around. A function that should do this would something like this:

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);
}
bfranco
  • 176
  • 7
  • that's great man. Any suggestions on how I should alter my question in order to fit the solution you gave? You obviously understood it :D – Roddeh May 17 '17 at 12:22
  • This answers the question, but array manipulations are really slow though. Please look at my answer, I used the PHP iterator to create a "internal pointer". **It is more convoluted than your answer though.** – Dymen1 May 17 '17 at 12:26
  • 3
    Yes, Dymen1, yours should be faster. About 4 times faster it seems. I've thrown them both in a small benchmark. I did 1mil iterations, your solution completed them in 0.18819093704224 seconds, while mine did in 0.67791223526001 seconds. The question is then, is this worth the extra development time? Yes, if this needs to be run thousands/millions of times in a short timespan, else I would tend to call it premature optimization. But it doesn't hurt to think about these situations. – bfranco May 17 '17 at 12:46
1

This might be a bit overkill for you:

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());
Dymen1
  • 738
  • 4
  • 17
  • This doesn't look like php too my even XD. I'm not going to use this, since it is indeed overkill for me, I don't understand it :P – Roddeh May 17 '17 at 15:17
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);
Shaunak Shukla
  • 2,347
  • 2
  • 18
  • 29
  • thanks man, this works also. @mi6crazyheart seemed a bit shorter and includes only one function. – Roddeh May 17 '17 at 12:12
  • 1
    According to me, @bfranco 's solution is the best with array functions and no loops and only four lines of function!!! If I have to choose answer I will choose him!! And if any answer is working for you, you should upvote them too, because they have spent time! Thanks!! – Shaunak Shukla May 17 '17 at 12:16
  • yep, absolutely true! – Roddeh May 17 '17 at 12:21
0

Check out this.

$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>';
Suresh
  • 5,687
  • 12
  • 51
  • 80