当前位置:网站首页>php 数组元素移动
php 数组元素移动
2022-08-03 05:26:00 【dd00bb】
<?php
$str = ['a','b','c','d'];
// 交换索引
function swapArray($arr, $index1, $index2) {
$arr[$index1] = array_splice($arr,$index2,1,$arr[$index1])[0];
return $arr;
}
// 上移 将当前数组index索引与后面一个元素互换位置,向数组后面移动一位
function moveUp($arr, $index) {
return swapArray($arr, $index, $index - 1);
}
// 下移 将当前数组index索引与前面一个元素互换位置,向数组前面移动一位
function moveDown($arr, $index) {
return swapArray($arr, $index, $index + 1);
}
var_dump(moveUp($str,2));
var_dump(moveDown($str,2));
?>
// 上移结果
Array
(
[0] => a
[1] => c
[2] => b
[3] => d
)
// 下移结果
Array
(
[0] => a
[1] => b
[2] => d
[3] => c
)
边栏推荐
猜你喜欢
随机推荐
用DirectX12绘制一个几何体的程序详述
2-php学习笔记之控制语句,函数
【面试准备】游戏开发中的几个岗位分别做什么&考察侧重点
【记录】把json的所有key转换成小写
二分查找5 - 第一个错误的版本
Automatic ticket issuance based on direct reduction of China Southern Airlines app
守望先锋英雄角色模型分享,obj文件+材质贴图,3dmax游戏建模
二层交换机,三层交换机,路由器内容总结记录
使用ZBrush制作恶魔模型
树——二叉排序树(BST)
申请公网ip后,配置光猫,路由器使用公网ip步骤
classpath:与classpath*的比较
八、抽象类的接口的区别
ZEMAX | 探索 OpticStudio中的序列模式
ZEMAX | 如何倾斜和偏心序列光学元件
9. Please introduce the class loading process, what is the parent delegation model?
关于芯片你了解吗?
电容器和电池有什么不同?
二分查找3 - 猜数字大小
域名管理常见问题:IP、域名和DNS之间的区别和关系









