Take a look at the following code:
There are two arrays before processing arrTitle
and arrHref
,
The arrTitle
contents are as follows:
arrHref
The contents are as follows:
//将title数组中首元素取出,作为栏目标题foreach ($arrTitle as &$title) { $text [] = $title[0]; unset($title[0]);}//将href数组中首元素取出,作为栏目urlforeach ($arrHref as &$href) { $url [] = $href[0]; unset($href[0]);}print_r($arrTitle);//重新组织title项$title = array_combine($text, $url);print_r($arrTitle);die;
Run the above PHP code to remove and remove the first element of each entry in title and href, but the problem is, before it executes array_combine
$arrTitle
:
After the execution array_combine
, however, $arrTitle
it became like this:
Why, $arrTitle
the last element becomes the array_combine()
result, and the array_combine()
function does not modify the $arrTitle
execution?
Reply content:
Take a look at the following code:
There are two arrays before processing arrTitle
and arrHref
,
The arrTitle
contents are as follows:
arrHref
The contents are as follows:
//将title数组中首元素取出,作为栏目标题foreach ($arrTitle as &$title) { $text [] = $title[0]; unset($title[0]);}//将href数组中首元素取出,作为栏目urlforeach ($arrHref as &$href) { $url [] = $href[0]; unset($href[0]);}print_r($arrTitle);//重新组织title项$title = array_combine($text, $url);print_r($arrTitle);die;
Run the above PHP code to remove and remove the first element of each entry in title and href, but the problem is, before it executes array_combine
$arrTitle
:
After the execution array_combine
, however, $arrTitle
it became like this:
Why, $arrTitle
the last element becomes the array_combine()
result, and the array_combine()
function does not modify the $arrTitle
execution?
**$title** = array_combine($text, $url);
Here the $title and the above cycle of the $title duplicate name, change the names of the good. There is no block scope in PHP.
This bug has been lifted, thanks to @whyreal point out the duplicate name problem.
Because in the Foreach loop, to 引用方式
iterate through the array, when the loop ends, it $title
points to $arrTitle
the last set of elements.
Since PHP has no block-level scope, at $title = Array_combine ($arr 1, $arr 2), the $title modifies the last set of elements it points to, resulting in $arrTitle
a bug.
Modify $title
the name that follows to dismiss the bug.