splice
adds and/or removes array elements
Syntax
splice(index, num, item1, ...)
Returns
Nothing
Parameters
- index (integer)
- The position to add/remove items.
- num (integer)
- Optional, number of items to be removed.
- Item (var)
- Optional, new elements(s) to be added.
Usage
Add item
var data = ['this', 'is', 'an', 'example', 'array'];
data.splice(2, 0, "not");
echo( objectToString(data) );
/* data will now contain
[
"this",
"is",
"not",
"an",
"example",
"array"
]
*/
First two items only
var data = ['this', 'is', 'an', 'example', 'array'];
data.splice(2 );
echo( objectToString(data) );
/* data will now contain
[
"this",
"is"
]
*/
Remove the first two items
var data = ['this', 'is', 'an', 'example', 'array'];
data.splice(0, 2);
echo( objectToString(data) );
/* data will now contain
[
"an",
"example",
"array"
]
*/
Last item only
var data = ['this', 'is', 'an', 'example', 'array'];
data.splice(0, data.length-1 );
echo( objectToString(data) );
}
/* data will now contain
[
"array"
]
*/
Remove last item
var data = ['this', 'is', 'an', 'example', 'array'];
data.splice(-1, 1 );
echo( objectToString(data) );
/* data will now contain
[
"this",
"is",
"an",
"example"
]
*/