foreach of usage &

The original address: https://blog.csdn.net/qq_38287952/article/details/79468321

For example, to add a new element of the array.

Demand here is a statistical commodity income, you can use the &.

$pro_arr = array(    

          array('price' =>10 , 'count' => 100),
                array('price' =>20 , 'count' => 90 )
          );   

1. The first method:

foreach ($pro_arr as $key=>$val) {
    $pro_arr[$key]['total'] = $val['price']*$val['count'];
}

2. The second method:

foreach ($pro_arr as &$val) {
    $val['total']=$val['price']*$val['count'];
}

Print Results:

    print_r($pro_arr);
    echo '</pre>';

The result is the same

 

Guess you like

Origin www.cnblogs.com/wangyingyao/p/11344944.html