指数在PHP数组推动元件和移动previous元 [英] php array push element at an index and move the previous element

查看:165
本文介绍了指数在PHP数组推动元件和移动previous元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数组。我想在索引3推的元件,并在同一时间移动至previous元件下。 请阅读第一它不是array_splice()工作

This is my array. I want to push an element at index 3 and at the same time move the previous element to next. Please read first its not array_splice() work

array(6) {
  [0]=>
  string(1) "One_test"
  [1]=>
  string(1) "Two_test"
  [2]=>
  string(1) "Three_test"
  [3]=>
  string(1) "Four_test"
  [4]=>
  string(1) "Five_test"
  [5]=>
  string(1) "Six_test"
}

所以,我期望的输出是

So my desired output is

array(6) {
  [0]=>
  string(1) "One_test"
  [1]=>
  string(1) "Two_test"
  [2]=>
  string(1) "Three_test"
  [3]=>
  string(1) "Six_test"
  [4]=>
  string(1) "Four_test"
  [5]=>
  string(1) "Five_test"
}

所以看到我需要用 5 索引元素,然后替换 3 索引元素的移动previously 3 索引元素进入下一。最后,推元件(5)删除

So notice I need replace 3rd indexed element with 5th indexed element and then move previously 3rd indexed element into next. Finally the pushed element (5th) to remove

任何想法?

推荐答案

从欺骗启发:在PHP中的任何位置插入数组新项目

我会做一个 array_pop() array_slice()阵列上:

$original = array( 'a', 'b', 'c', 'd', 'e' );
$new_one = array_pop($original);

array_splice( $original, 3, 0, $new_one );

我的解决方案

所以前:

array(6) {
  [0]=>
  string(8) "One_test"
  [1]=>
  string(8) "Two_test"
  [2]=>
  string(10) "Three_test"
  [3]=>
  string(9) "Four_test"
  [4]=>
  string(9) "Five_test"
  [5]=>
  string(8) "Six_test"
}

和之后:

array(6) {
  [0]=>
  string(8) "One_test"
  [1]=>
  string(8) "Two_test"
  [2]=>
  string(10) "Three_test"
  [3]=>
  string(8) "Six_test"
  [4]=>
  string(9) "Four_test"
  [5]=>
  string(9) "Five_test"
}

这篇关于指数在PHP数组推动元件和移动previous元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆