How can I insert a new item into an array on any position, for example in the middle of array?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
You may find this a little more intuitive. It only requires one function call to
array_splice
:A function that can insert at both integer and string positions:
Integer usage:
String Usage:
You can use this
This is what worked for me for the associative array:
The function source - this blog post. There's also handy function to insert BEFORE specific key.
This is also a working solution:
credits go to: http://binarykitten.com/php/52-php-insert-element-and-shift.html