Adding data to an array dynamically when the Foreach Loop

Source: Internet
Author: User

Today, when using TP to do a project, there is a problem, when foreach need to dynamically add data to the array, the sample code is as follows:

$arr Array (        array(' id ' = ' = ' string 1 ', ' name ' = ' = ' string 2 ', ' age ' = ' string 3 '),        array(' id ' = > ' String 4 ', ' name ' = = ' string 5 ', ' age ' = ' string 6 ')    ; foreach ($arras$v) {    $v[' sex '] = ' male ';} Var_dump ($arr);

The results are as follows:

Array (2) {  [0]=>  Array(3) {    ["id"]=> string    (10) "String 1"     ["name"]=> string    (10) "2"    ["Age"]=>    string( 10) "String 3"  }  [1]=>  Array(3) {    ["id"]=>     string (10) "Strings 4"    ["name"]=> string    (10) "Strings 5"    ["Age"]= >    string (10) "Strings 6"}  }

We can see that sex does not add success or see the data that is imagined, such as:

Array(2) {  [0]=>Array(4) {    ["id"]=>string(10) "String 1"    ["Name"]=>string(10) "String 2"    ["Age"]=>string(10) "String 3"    ["Sex"]=>string(3) "Male"  }  [1]=> &Array(4) {    ["id"]=>string(10) "String 4"    ["Name"]=>string(10) "String 5"    ["Age"]=>string(10) "String 6"    ["Sex"]=>string(3) "Male"  }}

Why is this? The Foreach loop is actually a copy of the array, not the array itself, if it is an array copy, it must be a copy of the array before the change, according to the results of the operation
Although the loop does change the original array, the loop is a copy of the array (that is, the old array), so you cannot loop to the newly added element.

The simple point is that your foreach array is a value pass , not a reference pass , if you do not know the value of the pass and the reference pass, please Baidu understand.

This causes you to add success when you print in foreach, and there is no data when used outside the loop, the sample code is as follows:

foreach($arr  as $v) {    $v[' sex '] = ' male '; Var_dump($v);}
The results are as followsArray(4) { ["id"]=>string(10) "String 1" ["Name"]=>string(10) "String 2" ["Age"]=>string(10) "String 3" ["Sex"]=>string(3) "Male"}Array(4) { ["id"]=>string(10) "String 4" ["Name"]=>string(10) "String 5" ["Age"]=>string(10) "String 6" ["Sex"]=>string(3) "Male"}

So it's also a good solution, before the variable with & declaration using reference passing, not value passing, there is also a point to note that only the named variable can pass the address assignment.

foreach($arr  as&$v) {    $v[' sex '] = ' male ';}Var_dump($arr);//The results are as followsArray(2) {  [0]=>Array(4) {    ["id"]=>string(10) "String 1"    ["Name"]=>string(10) "String 2"    ["Age"]=>string(10) "String 3"    ["Sex"]=>string(3) "Male"  }  [1]=> &Array(4) {    ["id"]=>string(10) "String 4"    ["Name"]=>string(10) "String 5"    ["Age"]=>string(10) "String 6"    ["Sex"]=>string(3) "Male"  }}

This is a problem I have encountered in my daily work, which is recorded here

If there are any errors in this article, I hope that you leave a message, learn from each other and make progress together.

The code word is not easy, reprint please attach this article connection.

Adding data to an array dynamically when the Foreach Loop

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.