PHP将在特定的索引 [英] PHP Insert at particular index

查看:111
本文介绍了PHP将在特定的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欲在第1索引移到数组present的元件阵列中第5索引。我该怎么做?

I want to move element of array present at 1st index to the 5th index in the array. How can I do that?

推荐答案

如果你真的是动,较能像这样

If you really mean "move", than that can be something like this

$from = 1;
$to = 5;
$el = $array[$from];
unset($array[$from]);
$array = array_merge (
    array_slice($array,0,$to),
    array($el),
    array_slice($array,$to));

不能测试它,但这个想法是:我们需要在 $从从原始数组中删除的元素,比我们在 $以和合并在一起。也许在 array_slice()某些指标不完全匹配;)

Cant test it, but the idea is: We take and remove the Element at $from from the original array, than we split the rest at $to and merge all together. Maybe some index in array_slice() does not match exactly;)

这篇关于PHP将在特定的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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