Home > Backend Development > PHP Tutorial > js php 数组比较

js php 数组比较

WBOY
Release: 2016-06-23 13:47:09
Original
1123 people have browsed it

   php 与 javascript 数组除了定义以及 操作上有很大的区别,还有很多其它的区别。现在我们就来讨论讨论。
   1.大家都知道php比较两个数组是否全相等(值,索引)相等

  $a=array(1,2,3);  $b=array(1,2,3);  echo ($a==$b);  //output 是1  如果是 javascript 呢:请看以下代码返回的:  <script type="text/javascript">      var a=['a','b','c'];      var b=['a','b','c'];      console.log(a==b);  //output 是false  </script>
Copy after login



 这是为什么呢?因为javascript 对象我们常成为引用类型,对象的比较均是引用的比较 ,当且它们引用同一基对象时,才相等。下面代码就是基于引用类型数组比较
   
      var a=['a','b','c'];      var b=a;      console.log(a==b);   //outupt 是true
Copy after login

 2.php和javascript 关于把数组对象赋值给一个变量也是不一样的。javascrpt 仅仅赋值的是引用值,对象本省并没有复制一次,而php赋值的是一个副本。可能说的不够明白,请看代码:

 <?php $a=array(1,2,3); $b=$a; $b[]=6; print_r($a); echo '<br/>'; print_r($b); //Array ( [0] => 1 [1] => 2 [2] => 3 )  //Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 6 )  js代码:      var a=['a','b','c'];      var b=a;      b[3]='d';      console.log(a);      console.log(b);      //output       //["a", "b", "c", "d"] index12.html:12      //["a", "b", "c", "d"] index12.html:13
Copy after login

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template