Table of Contents
Reply content:
Home Backend Development PHP Tutorial Why does PHP use the array_combine method to affect the original array?

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

Aug 04, 2016 am 09:19 AM
php

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?

//将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 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?

//将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 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.

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles