交换多维数组中的第一个和最后一个数组
Swap first and last array in multidimensional array
我有一个多维数组,格式如下
$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,依此类推。
谁能指出我正确的方向?
您可以只从数组中分割第一个和最后一个项目,然后 re-assemble 使用 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"
//}
我想你只是想移动顶部的标题数组,你可以像下面那样做一些圆形数组移动
$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);
输出
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
)
)
有很多方法可以给这只猫剥皮。这里有两个函数,第一个总是交换第一个和最后一个元素,第二个交换任意元素。我已经展示了如何使用该函数轻松交换首尾。您可以在 arraySwapFirstLast 函数中使用 arraySwap 函数中的逻辑而不是 array_shift, array_unshift, and array_pop,这可能会更有效,但这演示了一些重要的数组操作函数。 arraySwap 中的简单逻辑是您在教科书排序算法中使用的逻辑,非常高效。
<?php
$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'
)
);
$pet1 = $pet;
$pet2 = $pet;
/**
* Swap the first and last elements of an array
* Uses shift/unshift and standard array append
* @param $array
*/
function arraySwapFirstLast(&$array)
{
$first = array_shift($array);
$last = array_pop($array);
array_unshift($array, $last);
$array[] = $first;
}
/**
* Swap two arbitrary array elements
* @param $array
* @param $index1
* @param $index2
*/
function arraySwap(&$array, $index1, $index2)
{
$swapEl = $array[$index1];
$array[$index1] = $array[$index2];
$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;
你可以使用:
$arr1 = $pet[0];
$arr2 = $pet[count($pet)-1];
$pet[0] = $arr2;
$pet[count($pet)-1] = $arr1;
print_r($pet);
我有一个多维数组,格式如下
$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,依此类推。
谁能指出我正确的方向?
您可以只从数组中分割第一个和最后一个项目,然后 re-assemble 使用 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"
//}
我想你只是想移动顶部的标题数组,你可以像下面那样做一些圆形数组移动
$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);
输出
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
)
)
有很多方法可以给这只猫剥皮。这里有两个函数,第一个总是交换第一个和最后一个元素,第二个交换任意元素。我已经展示了如何使用该函数轻松交换首尾。您可以在 arraySwapFirstLast 函数中使用 arraySwap 函数中的逻辑而不是 array_shift, array_unshift, and array_pop,这可能会更有效,但这演示了一些重要的数组操作函数。 arraySwap 中的简单逻辑是您在教科书排序算法中使用的逻辑,非常高效。
<?php
$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'
)
);
$pet1 = $pet;
$pet2 = $pet;
/**
* Swap the first and last elements of an array
* Uses shift/unshift and standard array append
* @param $array
*/
function arraySwapFirstLast(&$array)
{
$first = array_shift($array);
$last = array_pop($array);
array_unshift($array, $last);
$array[] = $first;
}
/**
* Swap two arbitrary array elements
* @param $array
* @param $index1
* @param $index2
*/
function arraySwap(&$array, $index1, $index2)
{
$swapEl = $array[$index1];
$array[$index1] = $array[$index2];
$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;
你可以使用:
$arr1 = $pet[0];
$arr2 = $pet[count($pet)-1];
$pet[0] = $arr2;
$pet[count($pet)-1] = $arr1;
print_r($pet);