$input = Array ("A", "B", "C", "D", "E");
$output = Array_slice ($input, 2); Returns "C", "D", and "E"
- $output = Array_slice ($input,-2, 1); Returns "D"
- $output = Array_slice ($input, 0, 3); Returns "A", "B", and "C"
Note the differences in the array keys
- Print_r (Array_slice ($input, 2,-1));
- Print_r (Array_slice ($input, 2,-1, true));
- ?>
Copy CodeThe example above will output: Array ([0] = c[1] + d) Array ([2] = c[3] + D) 2.array_splice () Carry three parameters, ibid., to delete a subarray of length from offset. Example:
$input = Array ("Red", "green", "blue", "yellow");
- Array_splice ($input, 2);
- $input is now array ("Red", "green")
$input = Array ("Red", "green", "blue", "yellow");
- Array_splice ($input, 1,-1);
- $input is now array ("Red", "yellow")
$input = Array ("Red", "green", "blue", "yellow");
- Array_splice ($input, 1, Count ($input), "Orange");
- $input is now array ("Red", "orange")
$input = Array ("Red", "green", "blue", "yellow");
- Array_splice ($input,-1, 1, Array ("Black", "maroon"));
- $input is now array ("Red", "green",
- "Blue", "Black", "maroon")
$input = Array ("Red", "green", "blue", "yellow");
- Array_splice ($input, 3, 0, "purple");
- $input is now array ("Red", "green",
- "Blue", "purple", "yellow");
- ?>
Copy Code |