php的數組傳值調用與傳址調用

來源:互聯網
上載者:User
在java中數組的調用預設是是傳址調用:主體方法中傳遞一個數組給某個方法,在該方法內修改了數組的某個值;回到主方法中會發現數組已經發生了改變;

測試代碼:

public class ArrayTest {public static void main(String[] args){Map[] maparray=new Map[3];for (int i = 0; i < maparray.length; i++) {Map<String,String> map=new HashMap<String,String>();map.put("a", i+"_ajkcz");map.put("c", "werq_"+i);maparray[i]=map;}System.out.println("++++++++++++++++++++++++");for (int i = 0; i < maparray.length; i++) {Map<String,String> map=maparray[i];Iterator it=map.keySet().iterator();while(it.hasNext()){String key=(String) it.next(); System.out.println(key+"\t"+map.get(key) );}}System.out.println("++++++++++++++++++++++++");new ArrayCharge().printAndChangeArray(maparray);System.out.println("++++++++++++++++++++++++");for (int i = 0; i < maparray.length; i++) {Map<String,String> map=maparray[i];Iterator it=map.keySet().iterator();while(it.hasNext()){String key=(String) it.next(); System.out.println(key+"\t"+map.get(key) );}}}}class ArrayCharge {public void printAndChangeArray(Map[] maparray){for (int i = 0; i < maparray.length; i++) {Map<String,String> map=maparray[i];map.put("a",i+"________");}for (int i = 0; i < maparray.length; i++) {Map<String,String> map=maparray[i];Iterator it=map.keySet().iterator();while(it.hasNext()){String key=(String) it.next(); System.out.println(key+"\t"+map.get(key) );}}}}控制台輸出結果:++++++++++++++++++++++++cwerq_0a0_ajkczcwerq_1a1_ajkczcwerq_2a2_ajkcz++++++++++++++++++++++++cwerq_0a0________cwerq_1a1________cwerq_2a2________++++++++++++++++++++++++cwerq_0a0________cwerq_1a1________cwerq_2a2________

而在php中,數組的調用預設是傳值調用,在字方法中修改了數組,在父方法中無法察覺

測試代碼:

<?php $arraytest=array();for($i=0;$i<3;$i++){$child=array();$child['keystr']='key'.$i;$child['valuestr']='value'.$i;$arraytest[]=$child;}print_r($arraytest);print_r("+++++++++++++++++++++++++");for($i=0;$i<count($arraytest);$i++){$child=$arraytest[$i];$child['valuestr']="_________".$i;}print_r($arraytest);print_r("+++++++++++++++++++++++++");?>


控制台輸出:

Array ( [0] => Array (            [keystr] => key0            [valuestr] => value0 ) [1] => Array ( [keystr] => key1 [valuestr] => value1 ) [2] => Array ( [keystr] => key2 [valuestr] => value2 ) ) +++++++++++++++++++++++++Array ( [0] => Array ( [keystr] => key0 [valuestr] => value0 ) [1] => Array ( [keystr] => key1 [valuestr] => value1 ) [2] => Array ( [keystr] => key2 [valuestr] => value2 ) ) +++++++++++++++++++++++++

如果希望在php中也能類似java中傳數組參數給字方法,字方法處理完成後父方法中數組也跟隨改變則需要在傳遞參數時不傳遞值而是傳遞值的指標,例如p的值是100,即$p=100;在傳遞p給字方法時需要傳遞的參數則為“&$p”

測試代碼:

<?php $arraytest=array();for($i=0;$i<3;$i++){$child=array();$child['keystr']='key'.$i;$child['valuestr']='value'.$i;$arraytest[]=$child;}print_r($arraytest);print_r("+++++++++++++++++++++++++");for($i=0;$i<count($arraytest);$i++){$child=&$arraytest[$i]; //注意這裡加了一個指標符號,代表是傳址調用$child['valuestr']="_________".$i;}print_r($arraytest);print_r("+++++++++++++++++++++++++");?>


控制台輸出:

Array ( [0] => Array ( [keystr] => key0 [valuestr] => value0 ) [1] => Array ( [keystr] => key1 [valuestr] => value1 ) [2] => Array ( [keystr] => key2 [valuestr] => value2 ) ) +++++++++++++++++++++++++Array ( [0] => Array ( [keystr] => key0 [valuestr] => _________0 ) [1] => Array ( [keystr] => key1 [valuestr] => _________1 ) [2] => Array ( [keystr] => key2 [valuestr] => _________2 ) )
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.