Search code examples
phpmultidimensional-arrayswap

Change position of values in multidimensional array


I have the following multidimensional array. Need to switch the last value into position 3 and then push all the other values bellow that position. The current array

Array
    (
        [0] => Array
            (
                [0] => Vaccination
                [1] => Date of entry
                [2] => Animal
                [3] => Dog
                [4] => 246
                [5] => 4
                [6] => 984
                [7] => 102
            )

        [1] => Array
            (
                [0] => Vaccination
                [1] => Date of entry
                [2] => Animal
                [3] => Cat
                [4] => 246
                [5] => 4
                [6] => 984
                [7] => 118
            )
    )

Should output

Array
(
    [0] => Array
        (
            [0] => Vaccination
            [1] => Date of entry
            [2] => Animal
            [3] => 102
            [4] => Dog
            [5] => 246
            [6] => 4
            [7] => 984

        )

    [1] => Array
        (
            [0] => Vaccination
            [1] => Date of entry
            [2] => Animal
            [3] => 118
            [4] => Cat
            [5] => 246
            [6] => 4
            [7] => 984
        )
)

This can be done with some function where in the future you can enter any multi array, position to be swapped and current item to be swapped. Can somebody help me solve the problem?

I tried the following and might have over complicated things

function arrayPositionSwap($array, $from, $swap) {
    $s = $swap;
    foreach ($array as $key) {    
        foreach ($key as $value) {
            while ($s < $from) {
                $temp = $key[$s];
                $tempBellow = $key[$from];
                $key[$s] = $tempBellow;
                if($s == $s-1) {
                    $key[$from] = $temp;
                } else {
                    $tempb = $key[$s+1];
                    $key[$from] = $tempb;
                }
                $s++;
            }
        }
    }
} 

Solution

  • You can use array slices and end of array to achieve the same,

    foreach ($arr as $key => &$value) {
        $value = array_merge(array_slice($value, 0,3), [end($value)],  
                 array_slice($value, 3,-1));
    }
    

    Demo

    Output:-

    Array
    (
        [0] => Array
            (
                [0] => Vaccination
                [1] => Date of entry
                [2] => Animal
                [3] => 102
                [4] => Dog
                [5] => 246
                [6] => 4
                [7] => 984
            )
    
        [1] => Array
            (
                [0] => Vaccination
                [1] => Date of entry
                [2] => Animal
                [3] => 118
                [4] => Cat
                [5] => 246
                [6] => 4
                [7] => 984
            )
    
    )