欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php数组传值调用与传址调用

程序员文章站 2022-05-16 17:05:40
...
  1. public class ArrayTest {

  2. public static void main(String[] args){
  3. Map[] maparray=new Map[3];
  4. for (int i = 0; i Map map=new HashMap();
  5. map.put("a", i+"_ajkcz");
  6. map.put("c", "werq_"+i);
  7. maparray[i]=map;
  8. }
  9. System.out.println("++++++++++++++++++++++++");
  10. for (int i = 0; i Map map=maparray[i];
  11. Iterator it=map.keySet().iterator();
  12. while(it.hasNext()){
  13. String key=(String) it.next();
  14. System.out.println(key+"\t"+map.get(key) );
  15. }
  16. }
  17. System.out.println("++++++++++++++++++++++++");
  18. new ArrayCharge().printAndChangeArray(maparray);
  19. System.out.println("++++++++++++++++++++++++");
  20. for (int i = 0; i Map map=maparray[i];
  21. Iterator it=map.keySet().iterator();
  22. while(it.hasNext()){
  23. String key=(String) it.next();
  24. System.out.println(key+"\t"+map.get(key) );
  25. }
  26. }
  27. }
  28. }
  29. class ArrayCharge {

  30. public void printAndChangeArray(Map[] maparray){
  31. for (int i = 0; i Map map=maparray[i];
  32. map.put("a",i+"________");
  33. }
  34. for (int i = 0; i Map map=maparray[i];
  35. Iterator it=map.keySet().iterator();
  36. while(it.hasNext()){
  37. String key=(String) it.next();
  38. System.out.println(key+"\t"+map.get(key) );
  39. }
  40. }
  41. }
  42. }
复制代码

控制台输出结果:

  1. $arraytest=array();
  2. for($i=0;$i $child=array();
  3. $child['keystr']='key'.$i;
  4. $child['valuestr']='value'.$i;
  5. $arraytest[]=$child;
  6. }
  7. print_r($arraytest);
  8. print_r("+++++++++++++++++++++++++");
  9. for($i=0;$i $child=$arraytest[$i];
  10. $child['valuestr']="_________".$i;
  11. }
  12. print_r($arraytest);
  13. print_r("+++++++++++++++++++++++++");
  14. ?>
复制代码

控制台输出:

  1. $arraytest=array();
  2. for($i=0;$i $child=array();
  3. $child['keystr']='key'.$i;
  4. $child['valuestr']='value'.$i;
  5. $arraytest[]=$child;
  6. }
  7. print_r($arraytest);
  8. print_r("+++++++++++++++++++++++++");
  9. for($i=0;$i $child=&$arraytest[$i]; //注意这里加了一个指针符号,代表是传址调用
  10. $child['valuestr']="_________".$i;
  11. }
  12. print_r($arraytest);
  13. print_r("+++++++++++++++++++++++++");
  14. ?>
复制代码

控制台输出:

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 ) )+++++++++++++++++++++++++