標籤:style blog ar color 使用 sp for on div
PHP中支援使用引用‘&‘,用法與C基本一樣,個人理解就是函數中引用的變數指標直接指向了傳入參數的源地址,所以使用引用還是存在一定的危險性。所以對於一重迴圈,建議不使用引用,直接修改原array即可
$table_exchange=array(); array_push($table_exchange, array( "cnid" => ‘123‘, "status" => 0, "checked" => false, "leaf" => true )); foreach ($table_exchange as $b=> $c) { $table_exchange[$b][‘cnid‘]= ‘222‘; } echo json_encode($table_exchange);
輸出:
[{"cnid":"222","status":0,"checked":false,"leaf":true}]
而在操作複雜的多重迴圈中,使用引用會方便許多,也更加便於理解和操作,例如:
$nodeList=array(); array_push($nodeList, array( "cnid" => ‘1‘, "status" => 1, "checked" => false, "leaf" => true )); $table_exchange=array(); array_push($table_exchange, array( "cnid" => ‘2‘, "status" => 0, "checked" => false, "children" => $nodeList, "leaf" => false )); foreach ($table_exchange as $b=>& $c){ foreach($c[‘children‘] as $b2=>& $d){ $d[‘cnid‘]=‘000‘; } } echo json_encode($table_exchange);//轉成json格式輸出到網頁顯示結果
輸出:[{"cnid":"2","status":0,"checked":false,"children":[{"cnid":"000","status":1,"checked":false,"leaf":true}],"leaf":true}]
PHP 關於foreach 中修改array中元素的值