js php 數組比較

來源:互聯網
上載者:User

js php 數組比較
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>



這是為什麼呢?因為javascript 對象我們常成為參考型別,對象的比較均是引用的比較 ,當且它們引用同一基底物件時,才相等。下面代碼就是基於參考型別數組比較
      var a=['a','b','c'];      var b=a;      console.log(a==b);   //outupt 是true

2.php和javascript 關於把數組對象賦值給一個變數也是不一樣的。javascrpt 僅僅賦值的是引用值,對象本省並沒有複製一次,而php賦值的是一個副本。可能說的不夠明白,請看代碼:

 '; 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.