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.arrTitleAndarrHref,
WherearrTitleThe content is as follows:
arrHrefThe 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_combineBefore,$arrTitleYes:
Howeverarray_combineAfter,$arrTitleIt becomes like this:
Why,$arrTitleThe last elementarray_combine()Andarray_combine()The function does not$arrTitleModified?
Reply content:
See the following code:
There are two arrays before processing.arrTitleAndarrHref,
WherearrTitleThe content is as follows:
arrHrefThe 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_combineBefore,$arrTitleYes:
Howeverarray_combineAfter,$arrTitleIt becomes like this:
Why,$arrTitleThe last elementarray_combine()Andarray_combine()The function does not$arrTitleModified?
**$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 MethodTraverses the array. when the loop ends,$titlePointed$arrTitle.
Because PHP does not have block-level scope, in $ title = array_combine ($ arr1, $ arr2), this $ title simultaneously modifies$arrTitleThe last group of elements, resulting in a bug.
After modification$titleTo remove this bug.