php 數組去重

來源:互聯網
上載者:User

標籤:一鍵   數組   imp   二維數組   [1]   style   foreach   函數   cti   

php 數組去重 (2011-04-13 18:03:53) 轉載▼
標籤: 雜談  分類: javascript
數組中重複項的去除2010-07-28 15:29

一維數組的重複項

       使用array_unique函數即可,使用執行個體如下:

               <?php
                   $aa=array("apple","banana","pear","apple","wail","watermalon");
                   $bb=array_unique($aa);
                   print_r($bb);
                ?>

       結果如下:Array ( [0] => apple [1] => banana [2] => pear [4] => wail [5] => watermalon ) 。

二維數組的重複項

      對於二維數組咱們分兩種情況討論,一種是因為某一鍵名的值不能重複,重複資料刪除項;另一種因為內部的一維數組不能完全相同,而重複資料刪除項,下面舉例說明:

            ㈠因為某一鍵名的值不能重複,重複資料刪除項

           <?php
            function assoc_unique($arr, $key)
             {
               $tmp_arr = array();
               foreach($arr as $k => $v)
              {
                 if(in_array($v[$key], $tmp_arr))//搜尋$v[$key]是否在$tmp_arr數組中存在,若存在返回true
                {
                   unset($arr[$k]);
                }
              else {
                  $tmp_arr[] = $v[$key];
                }
              }
            sort($arr); //sort函數對數組進行排序
            return $arr;
            }

            $aa = array(
            array(‘id‘ => 123, ‘name‘ => ‘張三‘),
            array(‘id‘ => 123, ‘name‘ => ‘李四‘),
            array(‘id‘ => 124, ‘name‘ => ‘王五‘),
            array(‘id‘ => 125, ‘name‘ => ‘趙六‘),
            array(‘id‘ => 126, ‘name‘ => ‘趙六‘)
            );
            $key = ‘id‘;
            assoc_unique(&$aa, $key);
            print_r($aa);
            ?>

      顯示結果為:Array ( [0] => Array ( [id] => 123 [name] => 張三 ) [1] => Array ( [id] => 124 [name] => 王五 ) [2] => Array ( [id] => 125 [name] => 趙六 ) [3] => Array ( [id] => 126 [name] => 趙六 ) )

        ㈡因內部的一維數組不能完全相同,而重複資料刪除項

            <?php
            function array_unique_fb($array2D){
                 foreach ($array2D as $v){
                     $v = join(",",$v); //降維,也可以用implode,將一維數群組轉換為用逗號串連的字串
                     $temp[] = $v;
                 }
                 $temp = array_unique($temp);    //去掉重複的字串,也就是重複的一維數組
                foreach ($temp as $k => $v){
                    $temp[$k] = explode(",",$v);   //再將拆開的數組重新組裝
                }
                return $temp;
            }

            $aa = array(
            array(‘id‘ => 123, ‘name‘ => ‘張三‘),
            array(‘id‘ => 123, ‘name‘ => ‘李四‘),
            array(‘id‘ => 124, ‘name‘ => ‘王五‘),
            array(‘id‘ => 123, ‘name‘ => ‘李四‘),
            array(‘id‘ => 126, ‘name‘ => ‘趙六‘)
            );
            $bb=array_unique_fb($aa);
            print_r($bb)
            ?>

          顯示結果:Array ( [0] => Array ( [0] => 123 [1] => 張三 ) [1] => Array ( [0] => 123 [1] => 李四 ) [2] => Array ( [0] => 124 [1] => 王五 ) [4] => Array ( [0] => 126 [1] => 趙六 ) )  

php 數組去重

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.