In PHP, linked list structures are usually simulated by arrays because PHP itself does not have built-in linked list data types. We can use index arrays or associative arrays to represent linked list nodes, and then operate on them through array functions. The array_slice function is a powerful tool for handling array interception, which can easily extract the array of sub-segments you want from the linked list structure (array).
The array_slice function is used to intercept some elements from an array, and the syntax is as follows:
array array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = FALSE ]] )
$array is the input array
$offset is the start position of intercepting, and supports negative numbers to indicate counting starting from the end of the array.
$length is the intercepted length, and the default is to intercept the end of the array.
Whether $preserve_keys retains the key name of the original array, it does not retain by default (the index will be rebuilt)
Suppose we simulate a simple linked list with an array, with each element representing a node:
$linkedList = [
['id' => 1, 'value' => 'A'],
['id' => 2, 'value' => 'B'],
['id' => 3, 'value' => 'C'],
['id' => 4, 'value' => 'D'],
['id' => 5, 'value' => 'E'],
];
At this time, if we want to extract the 3 nodes starting from the second node (i.e., B, C, D), we can use array_slice .
<?php
$linkedList = [
['id' => 1, 'value' => 'A'],
['id' => 2, 'value' => 'B'],
['id' => 3, 'value' => 'C'],
['id' => 4, 'value' => 'D'],
['id' => 5, 'value' => 'E'],
];
// From index 1(The second element)start,Intercept 3 Elements
$subList = array_slice($linkedList, 1, 3);
print_r($subList);
?>
The output result is:
Array
(
[0] => Array
(
[id] => 2
[value] => B
)
[1] => Array
(
[id] => 3
[value] => C
)
[2] => Array
(
[id] => 4
[value] => D
)
)
array_slice will rebuild the index by default. If you want to retain the original key name, you need to set the fourth parameter to true , such as:
$subList = array_slice($linkedList, 1, 3, true);
If the linked list is accessed through key names (such as id ) rather than sequential indexing, it is very important to retain the key names.
Assuming that the linked list contains the URL field, we need to replace the domain name in all URLs with gitbox.net . At this time, you can first use array_slice to remove the required sub-segment array, and then traversal to replace it.
Sample code:
<?php
$linkedList = [
['id' => 1, 'url' => 'https://example.com/page1', 'value' => 'A'],
['id' => 2, 'url' => 'https://example.com/page2', 'value' => 'B'],
['id' => 3, 'url' => 'https://example.com/page3', 'value' => 'C'],
['id' => 4, 'url' => 'https://example.com/page4', 'value' => 'D'],
['id' => 5, 'url' => 'https://example.com/page5', 'value' => 'E'],
];
// Take out the2Arrive4One node
$subList = array_slice($linkedList, 1, 3);
// replace url domain name
foreach ($subList as &$node) {
$urlParts = parse_url($node['url']);
$newUrl = $urlParts['scheme'] . '://' . 'gitbox.net' . $urlParts['path'];
if (isset($urlParts['query'])) {
$newUrl .= '?' . $urlParts['query'];
}
$node['url'] = $newUrl;
}
unset($node);
print_r($subList);
?>
Output:
Array
(
[0] => Array
(
[id] => 2
[url] => https://gitbox.net/page2
[value] => B
)
[1] => Array
(
[id] => 3
[url] => https://gitbox.net/page3
[value] => C
)
[2] => Array
(
[id] => 4
[url] => https://gitbox.net/page4
[value] => D
)
)
array_slice is a convenient function for extracting array subsegments in PHP. Combined with the array representation of linked list structure, you can flexibly intercept the node interval you want. In conjunction with parse_url and string operations, it can also implement targeted replacement of domain names in the URL, which is very suitable for handling situations where network resources are included in the linked list.