**array_slice() Vs array_splice()**
What arrayslice() function do: As name suggest this function just return part/slice of array which we pass to arrayslice() function as parameter 1 depending upon the parameter 2 and parameter 3.
For e.g : we have any array:
$testarray=array("a","b","c","d","e","f","g","h");
$output=arrayslice($testarray,2,3);
Parameter 1: Simply the array
parameter 2: Index from where slicing of the $testarray array will start.
Parameter3: Length Upto which we have to slice the $testarray array .
The output of the above e.g will be:
array{
[0]=>c
[1]=>d
[2]=>e
}
Important :If parameter 2 is +ve then counting of index will start form the left side of the array starting with 0.
For e.g arrayslice($testarray,3,2)
As parameter 2 is +ve so we will start counting from left of $testarray upto 3rd index.As in $testarray at 3rd index value is 'd' .We will start counting from 'd' upto length = 2 (parameter 3) .So the output will be:
array{
[0]=>d
[1]=>e
}
But suppose parameter 2 is '-3'.So we will start counting from right end of array starting with 1 upto 3rd index so at 3rd index value is 'f' and length =2 so the output will be :
array{
[0]=>f
[1]=>g
}
arraysplice(): This function simply removes the part from the array which will be passed as parameter1 depending upon parameter2 and parameter3.
For e.g:
$testarray=array("a","b","c","d","e","f","g","h");
arraysplice($testarray,2,5);
echo "
";
printr($testarray);
Output: array{
[0]=>a
[1]=>b
[2]=>h
}
Description:as parameter 2 is +ve so start counting from left end of array starting with 0 ,so at index 2 value is 'c' and as length is 5 so we will remove following elements:c,d,e,f,g so except this value remaining will be left in the $testarray array array
.
This function will directly make changes in $testarray .
If parameter 2 is +ve or _ve : This will work same as arrayslice() explained above.
0 Comment(s)