如何将一个新项目插入到数组的任何位置,例如在数组的中间?
当前回答
这个由Brad Erickson编写的函数用于关联数组:
/* * Inserts a new key/value after the key in the array. * * @param $key * The key to insert after. * @param $array * An array to insert in to. * @param $new_key * The key to insert. * @param $new_value * An value to insert. * * @return * The new array if the key exists, FALSE otherwise. * * @see array_insert_before() */ function array_insert_after($key, array &$array, $new_key, $new_value) { if (array_key_exists($key, $array)) { $new = array(); foreach ($array as $k => $value) { $new[$k] = $value; if ($k === $key) { $new[$new_key] = $new_value; } } return $new; } return FALSE; }
函数来源-这篇博文。还有一个方便的功能,以插入之前特定的关键。
其他回答
杰伊的解决方案。李是完美的。如果要向多维数组中添加项,请先添加一个一维数组,然后再替换它。
$original = (
[0] => Array
(
[title] => Speed
[width] => 14
)
[1] => Array
(
[title] => Date
[width] => 18
)
[2] => Array
(
[title] => Pineapple
[width] => 30
)
)
将相同格式的项添加到此数组中将添加所有新的数组索引作为项,而不仅仅是项。
$new = array(
'title' => 'Time',
'width' => 10
);
array_splice($original,1,0,array('random_string')); // can be more items
$original[1] = $new; // replaced with actual item
注意:使用array_splice直接向多维数组添加项会将其所有索引作为项添加,而不仅仅是该项。
试试这个:
$colors = array('red', 'blue', 'yellow');
$colors = insertElementToArray($colors, 'green', 2);
function insertElementToArray($arr = array(), $element = null, $index = 0)
{
if ($element == null) {
return $arr;
}
$arrLength = count($arr);
$j = $arrLength - 1;
while ($j >= $index) {
$arr[$j+1] = $arr[$j];
$j--;
}
$arr[$index] = $element;
return $arr;
}
没有本地PHP函数(据我所知)可以完全完成您的请求。
我已经写了2个我认为适合目的的方法:
function insertBefore($input, $index, $element) {
if (!array_key_exists($index, $input)) {
throw new Exception("Index not found");
}
$tmpArray = array();
$originalIndex = 0;
foreach ($input as $key => $value) {
if ($key === $index) {
$tmpArray[] = $element;
break;
}
$tmpArray[$key] = $value;
$originalIndex++;
}
array_splice($input, 0, $originalIndex, $tmpArray);
return $input;
}
function insertAfter($input, $index, $element) {
if (!array_key_exists($index, $input)) {
throw new Exception("Index not found");
}
$tmpArray = array();
$originalIndex = 0;
foreach ($input as $key => $value) {
$tmpArray[$key] = $value;
$originalIndex++;
if ($key === $index) {
$tmpArray[] = $element;
break;
}
}
array_splice($input, 0, $originalIndex, $tmpArray);
return $input;
}
虽然速度更快,而且可能更节省内存,但这只适用于不需要维护数组键的地方。
如果你确实需要维护密钥,下面的方法会更合适;
function insertBefore($input, $index, $newKey, $element) {
if (!array_key_exists($index, $input)) {
throw new Exception("Index not found");
}
$tmpArray = array();
foreach ($input as $key => $value) {
if ($key === $index) {
$tmpArray[$newKey] = $element;
}
$tmpArray[$key] = $value;
}
return $input;
}
function insertAfter($input, $index, $newKey, $element) {
if (!array_key_exists($index, $input)) {
throw new Exception("Index not found");
}
$tmpArray = array();
foreach ($input as $key => $value) {
$tmpArray[$key] = $value;
if ($key === $index) {
$tmpArray[$newKey] = $element;
}
}
return $tmpArray;
}
通常,使用标量值:
$elements = array('foo', ...);
array_splice($array, $position, $length, $elements);
要在数组中插入一个数组元素,不要忘记将数组包装在数组中(因为它是一个标量值!):
$element = array('key1'=>'value1');
$elements = array($element);
array_splice($array, $position, $length, $elements);
否则,数组的所有键都将逐条添加。
这样你就可以插入数组:
function array_insert(&$array, $value, $index)
{
return $array = array_merge(array_splice($array, max(0, $index - 1)), array($value), $array);
}
推荐文章
- 在PHP单元测试执行期间,如何在CLI中输出?
- 在PHP中使用heredoc的优势是什么?
- PHP中的echo, print和print_r有什么区别?
- 如何在c#中连接列表?
- 在JavaScript中根据键值查找和删除数组中的对象
- 如何确定一个数组是否包含另一个数组的所有元素
- 如何将XML转换成PHP数组?
- 给定一个数字数组,返回所有其他数字的乘积的数组(不除法)
- 多维数组如何在内存中格式化?
- 如何将对象转换为数组?
- 从IP地址获取位置
- 在STL地图中,使用map::insert比[]更好吗?
- TypeScript枚举对象数组
- 获取数组值的键名
- HTTPS和SSL3_GET_SERVER_CERTIFICATE:证书验证失败,CA is OK