交换多维数组中的第一个和最后一个数组

我有以下格式的多维数组


$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'

    )

);

我需要将最后一个数组与第一个数组交换。我试图这样做


function arraySwap(&$array, $swap_a, $swap_b){

    list($array[$swap_a], $array[$swap_b]) = array($array[$swap_b], $array[$swap_a]);

}

这确实将数组首先与最后交换。然而,我需要的是推动阵列波纹管。因此,一旦我将最后一个数组与第一个交换,第一个需要到达位置 1,位置 1 处的数组变为 2,等等。


有人能指出我正确的方向吗?


慕后森
浏览 228回答 3
3回答

喵喵时光机

您可以只从数组中切出第一个和最后一个项目,然后使用array_merge.例子:function swapFirstAndLast($array) {    return array_merge(        array_slice($array, -1 , 1), // Last item        array_slice($array, 1 , count($array) - 2), // Second - Second last items        array_slice($array, 0 , 1) // First item    );}var_dump(array_column(swapFirstAndLast($pet), 'name'));//array(4) {//    [0] =>//  string(4) "Name"//    [1] =>//  string(9) "Spark Pug"//    [2] =>//  string(12) "Pico de Gato"//    [3] =>//  string(10) "Chew Barka"//}

阿晨1998

有很多方法可以给这只猫剥皮。这里有两个函数,第一个总是交换第一个和最后一个元素,第二个交换任意元素。我已经展示了如何使用该函数轻松地交换第一个和最后一个。您可以在 arraySwapFirstLast 函数中使用来自 arraySwap 函数的逻辑,而不是array_shift、array_unshift和 array_pop,它可能会更有效,但这演示了一些重要的数组操作函数。arraySwap 中的简单逻辑是您将在教科书排序算法中使用的,并且非常高效。<?php$pet = array(&nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; 'name' => 'Chew Barka',&nbsp; &nbsp; &nbsp; &nbsp; 'breed' => 'Bichon',&nbsp; &nbsp; &nbsp; &nbsp; 'age'&nbsp; => '2 years',&nbsp; &nbsp; &nbsp; &nbsp; 'weight' => 8,&nbsp; &nbsp; &nbsp; &nbsp; 'bio'&nbsp; &nbsp;=> 'The park, The pool or the Playground - I love to go anywhere! I am really great at... SQUIRREL!',&nbsp; &nbsp; &nbsp; &nbsp; 'filename' => 'pet1.png'&nbsp; &nbsp; ),&nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; 'name' => 'Spark Pug',&nbsp; &nbsp; &nbsp; &nbsp; 'breed' => 'Pug',&nbsp; &nbsp; &nbsp; &nbsp; 'age'&nbsp; => '1.5 years',&nbsp; &nbsp; &nbsp; &nbsp; 'weight' => 11,&nbsp; &nbsp; &nbsp; &nbsp; 'bio'&nbsp; &nbsp;=> 'You want to go to the dog park in style? Then I am your pug!',&nbsp; &nbsp; &nbsp; &nbsp; 'filename' => 'pet2.png'&nbsp; &nbsp; ),&nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; 'name' => 'Pico de Gato',&nbsp; &nbsp; &nbsp; &nbsp; 'breed' => 'Bengal',&nbsp; &nbsp; &nbsp; &nbsp; 'age'&nbsp; => '5 years',&nbsp; &nbsp; &nbsp; &nbsp; 'weight' => 9,&nbsp; &nbsp; &nbsp; &nbsp; 'bio'&nbsp; &nbsp;=> 'Oh hai, if you do not have a can of salmon I am not interested.',&nbsp; &nbsp; &nbsp; &nbsp; 'filename' => 'pet3.png'&nbsp; &nbsp; ),&nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; 'name' => 'Name',&nbsp; &nbsp; &nbsp; &nbsp; 'breed' => 'Breed',&nbsp; &nbsp; &nbsp; &nbsp; 'age'&nbsp; => 'Age',&nbsp; &nbsp; &nbsp; &nbsp; 'weight' => 'Weight',&nbsp; &nbsp; &nbsp; &nbsp; 'bio'&nbsp; &nbsp;=> 'Biography',&nbsp; &nbsp; &nbsp; &nbsp; 'filename' => 'Filename'&nbsp; &nbsp; ));$pet1 = $pet;$pet2 = $pet;/**&nbsp;* Swap the first and last elements of an array&nbsp;* Uses shift/unshift and standard array append&nbsp;* @param $array&nbsp;*/function arraySwapFirstLast(&$array){&nbsp; &nbsp; $first = array_shift($array);&nbsp; &nbsp; $last&nbsp; = array_pop($array);&nbsp; &nbsp; array_unshift($array, $last);&nbsp; &nbsp; $array[] = $first;}/**&nbsp;* Swap two arbitrary array elements&nbsp;* @param $array&nbsp;* @param $index1&nbsp;* @param $index2&nbsp;*/function arraySwap(&$array, $index1, $index2){&nbsp; &nbsp; $swapEl = $array[$index1];&nbsp; &nbsp; $array[$index1] = $array[$index2];&nbsp; &nbsp; $array[$index2] = $swapEl;}arraySwapFirstLast($pet);echo 'arraySwapFirstLast:'.PHP_EOL;print_r($pet).PHP_EOL;arraySwap($pet1, 1, 2);echo 'arraySwap second and third elements:'.PHP_EOL;print_r($pet1).PHP_EOL;arraySwap($pet2, 0, sizeof($pet2)-1);echo 'arraySwap first and last elements:'.PHP_EOL;print_r($pet2).PHP_EOL;

一只名叫tom的猫

我想你只是有兴趣在顶部移动标题阵列,你可以做一些圆形阵列移动,如下所示$arr = array(&nbsp; &nbsp; &nbsp;array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'name' => 'Chew Barka',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'breed' => 'Bichon',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'age'&nbsp; => '2 years',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'weight' => 8,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'bio'&nbsp; &nbsp;=> 'The park, The pool or the Playground - I love to go anywhere! I am really great at... SQUIRREL!',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'filename' => 'pet1.png'&nbsp; &nbsp; ),&nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'name' => 'Spark Pug',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'breed' => 'Pug',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'age'&nbsp; => '1.5 years',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'weight' => 11,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'bio'&nbsp; &nbsp;=> 'You want to go to the dog park in style? Then I am your pug!',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'filename' => 'pet2.png'&nbsp; &nbsp; ),&nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; 'name' => 'Pico de Gato',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'breed' => 'Bengal',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'age'&nbsp; => '5 years',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'weight' => 9,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'bio'&nbsp; &nbsp;=> 'Oh hai, if you do not have a can of salmon I am not interested.',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'filename' => 'pet3.png'&nbsp; &nbsp; ),&nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; 'name' => 'Name',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'breed' => 'Breed',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'age'&nbsp; => 'Age',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'weight' => 'Weight',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'bio'&nbsp; &nbsp;=> 'Biography',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'filename' => 'Filename'&nbsp; &nbsp; ));echo "<pre>";$key=count($arr)-1;$output1 = array_slice($arr, $key);&nbsp;$output2 = array_slice($arr, 0,$key);&nbsp;$new=array_merge($output1,$output2);print_r($new);输出Array(&nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [name] => Name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [breed] => Breed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [age] => Age&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [weight] => Weight&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [bio] => Biography&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [filename] => Filename&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [1] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [name] => Chew Barka&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [breed] => Bichon&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [age] => 2 years&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [weight] => 8&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [bio] => The park, The pool or the Playground - I love to go anywhere! I am really great at... SQUIRREL!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [filename] => pet1.png&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [2] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [name] => Spark Pug&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [breed] => Pug&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [age] => 1.5 years&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [weight] => 11&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [bio] => You want to go to the dog park in style? Then I am your pug!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [filename] => pet2.png&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [3] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [name] => Pico de Gato&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [breed] => Bengal&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [age] => 5 years&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [weight] => 9&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [bio] => Oh hai, if you do not have a can of salmon I am not interested.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [filename] => pet3.png&nbsp; &nbsp; &nbsp; &nbsp; ))
打开App,查看更多内容
随时随地看视频慕课网APP