& Amp; lt ;? Php $ items [& #039; apple & #039;, & #039; Orange & #039;, & #039; Li & #039;, & #039; pineapple & #039;, & #039; banana & #039;, & #039; dragon fruit & #039;]; * $ array passed in by items $ position of index to be inserted $ value data to be inserted * functioninsertAt ($ items, $ index, $ value) {code ...}} $ itemsinsertA...
$ Items = ['apple', 'orange', 'pe', 'pineapple ', 'Banana', 'Dragon go'];
/*
$ Array imported by items
$ Index location to be inserted
$ Value: the data to be inserted.
*/
Function insertAt ($ items, $ index, $ value ){
return $items;
}
$ Items = insertAt ($ items, 2, 'orange'); // after the execution is complete, the result is as follows:
['Apple', 'orange', 'orange', 'pear ', 'pineapple', 'Banana ', 'Dragon go'];
Reply content:
$ Items = ['apple', 'orange', 'pe', 'pineapple ', 'Banana', 'Dragon go'];
/*
$ Array imported by items
$ Index location to be inserted
$ Value: the data to be inserted.
*/
Function insertAt ($ items, $ index, $ value ){
return $items;
}
$ Items = insertAt ($ items, 2, 'orange'); // after the execution is complete, the result is as follows:
['Apple', 'orange', 'orange', 'pear ', 'pineapple', 'Banana ', 'Dragon go'];
php
String 'apple' (length = 6) 1 => string 'orange' (length = 6) 2 => string 'orange' (length = 6) 3 => string 'pe' (length = 3) 4 => string 'pineapple '(length = 6) 5 => string 'banana' (length = 6) 6 => string 'Dragon go' (length = 9 )*/
@ Song XiaoBei refers to the native Implementation of array_splice in php. We must never duplicate the wheel.
Create one here 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);}