Home >Backend Development >PHP Tutorial >PHP array call by value and call by address

PHP array call by value and call by address

WBOY
WBOYOriginal
2016-07-25 08:53:291159browse
  1. public class ArrayTest {

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

  32. class ArrayCharge {

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

控制台输出结果:

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

控制台输出:

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

控制台输出:

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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn