Home > Backend Development > PHP Tutorial > php foreach array_merge problems and confusion

php foreach array_merge problems and confusion

WBOY
Release: 2016-07-06 13:53:19
Original
1249 people have browsed it

Code:

<code>$a = array('a' => array('a'));
$b = array('b' => array('b'));

foreach ($b as $key => &$item) {
    $item[] = 'd';
}
$a = array_merge($b, $a);
$b['b'][] = 'c';
print_r($a);
print_r($b);</code>
Copy after login
Copy after login

Result:

<code>Array
(
    [b] => Array
        (
            [0] => b
            [1] => d
            [2] => c
        )
    [a] => Array
        (
            [0] => a
        )
)
Array
(
    [b] => Array
        (
            [0] => b
            [1] => d
            [2] => c
        )
)</code>
Copy after login
Copy after login

My confusion is why the operation on $b affects $a after the merger.
Anyone who knows, please clarify, thank you!

Reply content:

Code:

<code>$a = array('a' => array('a'));
$b = array('b' => array('b'));

foreach ($b as $key => &$item) {
    $item[] = 'd';
}
$a = array_merge($b, $a);
$b['b'][] = 'c';
print_r($a);
print_r($b);</code>
Copy after login
Copy after login

Result:

<code>Array
(
    [b] => Array
        (
            [0] => b
            [1] => d
            [2] => c
        )
    [a] => Array
        (
            [0] => a
        )
)
Array
(
    [b] => Array
        (
            [0] => b
            [1] => d
            [2] => c
        )
)</code>
Copy after login
Copy after login

My confusion is why the operation on $b affects $a after the merger.
Anyone who knows, please clarify, thank you!

Actually, I think you can see it if you change print_r to var_dump

<code>array (size=2)
  'b' => &
    array (size=3)
      0 => string 'b' (length=1)
      1 => string 'd' (length=1)
      2 => string 'c' (length=1)
  'a' => 
    array (size=1)
      0 => string 'a' (length=1)
array (size=1)
  'b' => &
    array (size=3)
      0 => string 'b' (length=1)
      1 => string 'd' (length=1)
      2 => string 'c' (length=1)</code>
Copy after login

Variable b points to a reference type, so if the value of b is changed, the value of b in a will also change

Thank you for the reply from @风咷horn. I googled “php foreach reference” again and found this article, which gave me a better understanding of this issue

[PHP] An interview question about variable reference in foreach loop

Let me tell you my understanding. When using the & symbol, the variables $b and $item will point to an address at the same time. At this time, $b becomes a reference variable. If $item is unset after foreach, this will The reference of $b will be dereferenced.

Related labels:
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