Why does the array_combine method affect the original array in PHP?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.