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

PHP array call by value and call by address

巴扎黑
巴扎黑Original
2016-11-12 14:00:221827browse

The calling of arrays in Java is by default called by address: in the main method, an array is passed to a method, and a certain value of the array is modified in the method; when you return to the main method, you will find that the array has changed;

Test code:

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________

In php, array calls are by value by default. Modifying the array in the submethod cannot be detected in the parent method

Test code:

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


Console output:

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

If you want to be able to pass array parameters to the word method in PHP similarly to Java, and the array in the parent method will also change after the word method is processed, you need to pass the value instead of the value when passing the parameter. pointer, for example, the value of p is 100, that is, $p=100; when passing p to the word method, the parameter that needs to be passed is "&$p"

Test code:

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


Console output :

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