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

巴扎黑
發布: 2016-11-12 14:00:22
原創
1831 人瀏覽過

在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_0
a0_ajkcz
cwerq_1
a1_ajkcz
cwerq_2
a2_ajkcz
++++++++++++++++++++++++
cwerq_0
a0________
cwerq_1
a1________
cwerq_2
a2________
++++++++++++++++++++++++
cwerq_0
a0________
cwerq_1
a1________
cwerq_2
a2________
登入後複製

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

 測試程式碼:

控制台輸出:

<?php 
$arraytest=array();
for($i=0;$i<3;$i++){
$child=array();
$child[&#39;keystr&#39;]=&#39;key&#39;.$i;
$child[&#39;valuestr&#39;]=&#39;value&#39;.$i;
$arraytest[]=$child;
}
print_r($arraytest);
print_r("+++++++++++++++++++++++++");
for($i=0;$i<count($arraytest);$i++){
$child=$arraytest[$i];
$child[&#39;valuestr&#39;]="_________".$i;
}
print_r($arraytest);
print_r("+++++++++++++++++++++++++");
?>
登入後複製

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

測試代碼:

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 
$arraytest=array();
for($i=0;$i<3;$i++){
$child=array();
$child[&#39;keystr&#39;]=&#39;key&#39;.$i;
$child[&#39;valuestr&#39;]=&#39;value&#39;.$i;
$arraytest[]=$child;
}
print_r($arraytest);
print_r("+++++++++++++++++++++++++");
for($i=0;$i<count($arraytest);$i++){
$child=&$arraytest[$i]; //注意这里加了一个指针符号,代表是传址调用
$child[&#39;valuestr&#39;]="_________".$i;
}
print_r($arraytest);
print_r("+++++++++++++++++++++++++");
?>
登入後複製

相關標籤:
php
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板