Old problems encountered during Database batch import yesterday (foreach + & related issues)

Source: Internet
Author: User
The old problem (foreach + & amp; problem) encountered during Database batch import yesterday can be summarized as: $ arr & nbsp ;=& nbsp; array, 3, 4, 5); foreach ($ arr & nbsp; as & nbsp; $ key & nbsp ;=& gt; & nbsp; & amp; $ row) & the old problem encountered during batch database import yesterday (foreach + & Problem)
The problem can be summarized as follows:
$arr = array(1,2,3,4,5);
foreach($arr as $key => &$row) {}
foreach($arr as $key => $row) {}
var_dump($arr);

array (size=5)
  0 => int 1
  1 => int 2
  2 => int 3
  3 => int 4
  4 => &int 4

Foreach pointer problem, pointer, please show up! Share :? & $ Row )? {} Foreach ($ arr? As? $ Ke... 'data-pics = ''>
------ Solution --------------------
$ Arr = array (1, 2, 3, 4, 5 );
Foreach ($ arr as $ key =>& $ row) {}// after this execution is complete, $ arr [4] is a reference with a value of 5
Foreach ($ arr as $ key => $ row) {}// this execution process means that the current value is assigned to $ arr [4] Each time, the value of $ arr [4] is 4 when the fourth round is completed. the fifth round is to assign a value to yourself, so it is 4.


$arr = array(1,2,3,4,5);
foreach($arr as $key => &$row) {}
var_dump($arr);
echo $arr[4].'
';
foreach($arr as $key => $row) {
echo $arr[4].'_';
}
var_dump($arr);

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.