$items = [' Apple ', ' orange ', ' pear ', ' pineapple ', ' banana ', ' dragon Fruit '];
/*
$items the incoming array
$index the location to insert
$value the data to be inserted
*/
function InsertAt ($items, $index, $value) {
return $items;
}
$items =insertat ($items, 2, ' oranges '); When the execution is complete, the results are as follows
[' Apples ', ' oranges ', ' oranges ', ' pears ', ' pineapple ', ' Bananas ', ' dragon Fruit '];
Reply content:
$items = [' Apple ', ' orange ', ' pear ', ' pineapple ', ' banana ', ' dragon Fruit '];
/*
$items the incoming array
$index the location to insert
$value the data to be inserted
*/
function InsertAt ($items, $index, $value) {
return $items;
}
$items =insertat ($items, 2, ' oranges '); When the execution is complete, the results are as follows
[' Apples ', ' oranges ', ' oranges ', ' pears ', ' pineapple ', ' Bananas ', ' dragon Fruit '];
php
string '苹果' (length=6) 1 => string '橘子' (length=6) 2 => string '橙子' (length=6) 3 => string '梨' (length=3) 4 => string '菠萝' (length=6) 5 => string '香蕉' (length=6) 6 => string '火龙果' (length=9) */
The Array_splice mentioned in song Xiao Bei is a native implementation of PHP. We must not make the wheel again.
Build one here, just for fun.
function insertAt($items, $value, $index=0) { is_numeric($index) and $items[($index-'0.1').'x'] = $value and ksort($items); return array_values($items);}