Why does PHP use the array_combine method to affect the original array?

WBOY
Release: 2016-08-04 09:19:29
Original
1351 people have browsed it

Please see the code below:

There are two arrays before processingarrTitleandarrHref,

The content ofarrTitleis as follows:

Why does PHP use the array_combine method to affect the original array?

arrHrefThe content is as follows:

Why does PHP use the array_combine method to affect the original array?

//将title数组中首元素取出,作为栏目标题 foreach ($arrTitle as &$title) { $text [] = $title[0]; unset($title[0]); } //将href数组中首元素取出,作为栏目url foreach ($arrHref as &$href) { $url [] = $href[0]; unset($href[0]); } print_r($arrTitle); //重新组织title项 $title = array_combine($text, $url); print_r($arrTitle);die;
Copy after login
Copy after login

Run the above PHP code to extract and remove the first element of each item in title and href. However, the problem arises. Before executingarray_combine,$arrTitlelooks like this:

Why does PHP use the array_combine method to affect the original array?

However, after executingarray_combine,$arrTitlebecomes like this:

Why does PHP use the array_combine method to affect the original array?

Why, the last element of$arrTitlebecomes the result ofarray_combine(), but thearray_combine()function does not modify$arrTitle?

Reply content:

Please see the code below:

There are two arrays before processingarrTitleandarrHref,

The content ofarrTitleis as follows:

Why does PHP use the array_combine method to affect the original array?

arrHrefThe content is as follows:

Why does PHP use the array_combine method to affect the original array?

//将title数组中首元素取出,作为栏目标题 foreach ($arrTitle as &$title) { $text [] = $title[0]; unset($title[0]); } //将href数组中首元素取出,作为栏目url foreach ($arrHref as &$href) { $url [] = $href[0]; unset($href[0]); } print_r($arrTitle); //重新组织title项 $title = array_combine($text, $url); print_r($arrTitle);die;
Copy after login
Copy after login

Run the above PHP code to extract and remove the first element of each item in title and href. However, the problem arises. Before executingarray_combine,$arrTitlelooks like this:

Why does PHP use the array_combine method to affect the original array?

However, after executingarray_combine,$arrTitlebecomes like this:

Why does PHP use the array_combine method to affect the original array?

Why, the last element of$arrTitlebecomes the result ofarray_combine(), but thearray_combine()function does not modify$arrTitle?

**$title** = array_combine($text, $url); 
Copy after login

$title here has the same name as $title in the loop above, just change the name. There is no block scope in php.

This bug has been resolved. Thanks to @whyreal for pointing out the duplicate name issue.

Since in the foreach loop, the array is traversed inreference mode, when the loop ends,$titlepoints to the last set of elements of$arrTitle.

Since PHP does not have a block-level scope, in $title = array_combine($arr1, $arr2), then the $title also modifies the last group of elements it points to$arrTitle, resulting in a bug.

Modify the name behind$titleto eliminate this bug.

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!