Search code examples
phpmultidimensional-arrayswap

Swap first and last array in multidimensional array


I have a multidimensional array in the following format

$pet = array(
     array(
            'name' => 'Chew Barka',
            'breed' => 'Bichon',
            'age'  => '2 years',
            'weight' => 8,
         'bio'   => 'The park, The pool or the Playground - I love to go anywhere! I am really great at... SQUIRREL!',
            'filename' => 'pet1.png'
    ),
    array(
            'name' => 'Spark Pug',
            'breed' => 'Pug',
            'age'  => '1.5 years',
            'weight' => 11,
            'bio'   => 'You want to go to the dog park in style? Then I am your pug!',
            'filename' => 'pet2.png'
    ),
    array(
        'name' => 'Pico de Gato',
            'breed' => 'Bengal',
            'age'  => '5 years',
            'weight' => 9,
            'bio'   => 'Oh hai, if you do not have a can of salmon I am not interested.',
            'filename' => 'pet3.png'
    ),
    array(
        'name' => 'Name',
            'breed' => 'Breed',
            'age'  => 'Age',
            'weight' => 'Weight',
            'bio'   => 'Biography',
            'filename' => 'Filename'
    )
);

I need to swap the last array with the first. I tried to do this

function arraySwap(&$array, $swap_a, $swap_b){
    list($array[$swap_a], $array[$swap_b]) = array($array[$swap_b], $array[$swap_a]);
}

This does swap the arrays first with last. However what i need is to push the arrays bellow. So once i swap the last array with the first, the first needs to come to position 1, the one at position 1 goes to 2 etc.

Can anybody point me in the right direction?


Solution

  • I think you are just interested to shift the heading array on the top, you can do some circular array shifting like below

    $arr = array(
         array(
                'name' => 'Chew Barka',
                'breed' => 'Bichon',
                'age'  => '2 years',
                'weight' => 8,
             'bio'   => 'The park, The pool or the Playground - I love to go anywhere! I am really great at... SQUIRREL!',
                'filename' => 'pet1.png'
        ),
        array(
                'name' => 'Spark Pug',
                'breed' => 'Pug',
                'age'  => '1.5 years',
                'weight' => 11,
                'bio'   => 'You want to go to the dog park in style? Then I am your pug!',
                'filename' => 'pet2.png'
        ),
        array(
            'name' => 'Pico de Gato',
                'breed' => 'Bengal',
                'age'  => '5 years',
                'weight' => 9,
                'bio'   => 'Oh hai, if you do not have a can of salmon I am not interested.',
                'filename' => 'pet3.png'
        ),
        array(
            'name' => 'Name',
                'breed' => 'Breed',
                'age'  => 'Age',
                'weight' => 'Weight',
                'bio'   => 'Biography',
                'filename' => 'Filename'
        )
    );
    
    echo "<pre>";
    $key=count($arr)-1;
    $output1 = array_slice($arr, $key); 
    $output2 = array_slice($arr, 0,$key); 
    $new=array_merge($output1,$output2);
    print_r($new);
    

    Output

    Array
    (
        [0] => Array
            (
                [name] => Name
                [breed] => Breed
                [age] => Age
                [weight] => Weight
                [bio] => Biography
                [filename] => Filename
            )
    
        [1] => Array
            (
                [name] => Chew Barka
                [breed] => Bichon
                [age] => 2 years
                [weight] => 8
                [bio] => The park, The pool or the Playground - I love to go anywhere! I am really great at... SQUIRREL!
                [filename] => pet1.png
            )
    
        [2] => Array
            (
                [name] => Spark Pug
                [breed] => Pug
                [age] => 1.5 years
                [weight] => 11
                [bio] => You want to go to the dog park in style? Then I am your pug!
                [filename] => pet2.png
            )
    
        [3] => Array
            (
                [name] => Pico de Gato
                [breed] => Bengal
                [age] => 5 years
                [weight] => 9
                [bio] => Oh hai, if you do not have a can of salmon I am not interested.
                [filename] => pet3.png
            )
    
    )