当前位置:网站首页>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
)
边栏推荐
猜你喜欢
随机推荐
STM32启动文件的选择
Dynamic adjustment of web theme (2) Extraction
【面试】摸鱼快看:关于selenium/ui自动化的面试题
稳压二极管的工作原理及稳压二极管使用电路图
数组与字符串8-最长回文子串
MATLAB给多组条形图添加误差棒
借助ginput函数在figure窗口实时读取、展示多条曲线的坐标值
ZEMAX | 如何创建复杂的非序列物体
二、Exception和Error有什么区别?
守望先锋英雄角色模型分享,obj文件+材质贴图,3dmax游戏建模
最优化方法概述
9. Please introduce the class loading process, what is the parent delegation model?
【随笔】把喜欢的事情提上日程吧
Automatic ticket issuance based on direct reduction of China Southern Airlines app
window下VS2022封装动态库以及调用动态库
看了都收藏的3D游戏建模全流程解析,角色模型就该这么做!
数组与字符串11-反转字符串
cb板上常用的电子元器件都有哪些?
【面试准备】游戏开发中的几个岗位分别做什么&考察侧重点
域名注册流程:如何选择购买合适的域名?









