See the following code: before processing, there are two arrays arrTitle and arrHref. the arrTitle content is as follows: arrHref content: {code ...} run the above PHP code to retrieve and remove the first element of each item in title and href. However, the problem arises. in the execution of array_combine...
See the following code:
There are two arrays before processing.arrTitle
AndarrHref
,
WherearrTitle
The content is as follows:
arrHref
The content is as follows:
// Retrieve the first element from the title array as the topic title foreach ($ arrTitle as & $ title) {$ text [] = $ title [0]; unset ($ title [0]);} // extracts the first element from the href array as the column urlforeach ($ arrHref as & $ href) {$ url [] = $ href [0]; unset ($ href [0]);} print_r ($ arrTitle ); // reorganize the title item $ title = array_combine ($ text, $ url); print_r ($ arrTitle); die;
Run the above PHP code to retrieve and remove the first element of each item in title and href. However, the problem arises.array_combine
Before,$arrTitle
Yes:
Howeverarray_combine
After,$arrTitle
It becomes like this:
Why,$arrTitle
The last elementarray_combine()
Andarray_combine()
The function does not$arrTitle
Modified?
Reply content:
See the following code:
There are two arrays before processing.arrTitle
AndarrHref
,
WherearrTitle
The content is as follows:
arrHref
The content is as follows:
// Retrieve the first element from the title array as the topic title foreach ($ arrTitle as & $ title) {$ text [] = $ title [0]; unset ($ title [0]);} // extracts the first element from the href array as the column urlforeach ($ arrHref as & $ href) {$ url [] = $ href [0]; unset ($ href [0]);} print_r ($ arrTitle ); // reorganize the title item $ title = array_combine ($ text, $ url); print_r ($ arrTitle); die;
Run the above PHP code to retrieve and remove the first element of each item in title and href. However, the problem arises.array_combine
Before,$arrTitle
Yes:
Howeverarray_combine
After,$arrTitle
It becomes like this:
Why,$arrTitle
The last elementarray_combine()
Andarray_combine()
The function does not$arrTitle
Modified?
**$title** = array_combine($text, $url);
The $ title Here is the same as the $ title in the previous loop. just change the name. Php does not have block scope.
This bug has been removed. thanks @ whyreal for pointing out the duplicate name problem.
In the foreach loopReference Method
Traverses the array. when the loop ends,$title
Pointed$arrTitle
.
Because PHP does not have block-level scope, in $ title = array_combine ($ arr1, $ arr2), this $ title simultaneously modifies$arrTitle
The last group of elements, resulting in a bug.
After modification$title
To remove this bug.