The PHP foreach Loop uses the "& $val" address character "&"

Source: Internet
Author: User
Tags php foreach

See this foreach loop when you are familiar with the project code:

foreach($data as &$val) {

....

}

The first time I saw the loop, I used the address sign "&", and the symbol in my impression was taken directly to the address of the variable.

Then the inside of the loop operation, relatively tall on the point, such as the following example:

I now have a need to add an element to an array, often with this requirement
Commodity statistics: Commodity prices, merchandise sales quantity
The demand is to count the total revenue of the goods

$arr=Array(Array ( ' price '  => 10 ,  ' count '  => 100  array ( ' price '  =>20   ' count '  =>  90 )

Instead of using a new array to store the results, insert the old array directly into the new array.

foreach ($arr as &$val) {

$val [' Total ']=$val[']* '$val ['count '];

}

In fact, the following code is no different:

foreach ($arr as $k = =$val) {
$arr [$k] [' Total ']= $val [']* ' $val [' count '];
}

But I think the grid may be high.

The PHP foreach Loop uses the "& $val" address character "&"

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.