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

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

Please see the code below:

There are two arrays before processing arrTitle and arrHref,

The content of arrTitle is 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?

<code class="php">//将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;</code>
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 executing array_combine, $arrTitle looks like this:

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

However, after executing array_combine, $arrTitlebecomes like this:

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

Why, the last element of $arrTitle becomes the result of array_combine(), but the array_combine() function does not modify $arrTitle?

Reply content:

Please see the code below:

There are two arrays before processing arrTitle and arrHref,

The content of arrTitle is 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?

<code class="php">//将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;</code>
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 executing array_combine, $arrTitle looks like this:

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

However, after executing array_combine, $arrTitlebecomes like this:

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

Why, the last element of $arrTitle becomes the result of array_combine(), but the array_combine() function does not modify $arrTitle?

<code>**$title** = array_combine($text, $url);
</code>
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 in reference mode, when the loop ends, $title points 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 $title to 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
Popular Tutorials
More>
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!