Why PHP uses the Array_combine method to affect the original array

Source: Internet
Author: User

Take a look at the following code:

There are two arrays before processing arrTitle and arrHref ,

The arrTitle contents are as follows:

arrHrefThe 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:

arrHrefThe 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.

  • 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.