杭州PHP培训
达内杭州PHP培训中心

0571-56020837

热门课程

php的数组传值调用与传址调用详解!

  • 时间:2019-01-09 14:12
  • 发布:转载
  • 来源:网络

java中数组的调用默认是是传址调用:主体方法中传递一个数组给某个方法,在该方法内修改了数组的某个值;回到主方法中会发现数组已经发生了改变;

测试代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

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中,数组的调用默认是传值调用,在字方法中修改了数组,在父方法中无法察觉

测试代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<?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("+++++++++++++++++++++++++");

?>

 

控制台输出:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

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”

测试代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<?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("+++++++++++++++++++++++++");

?>

 

控制台输出:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

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

)

)

 

预约申请免费试听课

怕钱不够?就业挣钱后再付学费!    怕学不会?从入学起,达内定制课程!     担心就业?达内多家实践企业供你挑选!

上一篇:这些常见的PHP开发框架,肯定用得到!
下一篇:没有下一篇了
选择城市和中心
贵州省

广西省

海南省