在PHP中的任何位置插入数组新项目 [英] Insert new item in array on any position in PHP

查看:139
本文介绍了在PHP中的任何位置插入数组新项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能在阵的中间插入一个新的项目插入任何位置的阵列,例如?

How can I insert a new item into an array on any position, for example in the middle of array?

推荐答案

您可能会发现这是一个更直观一点。它只需要一个函数调用 array_splice

You may find this a little more intuitive. It only requires one function call to array_splice:

$original = array( 'a', 'b', 'c', 'd', 'e' );
$inserted = array( 'x' ); // Not necessarily an array

array_splice( $original, 3, 0, $inserted ); // splice in at position 3
// $original is now a b c x d e

这篇关于在PHP中的任何位置插入数组新项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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