php 數組函數

來源:互聯網
上載者:User

標籤:返回結果   merge   world   uniq   txt   數組   數組函數   索引   imp   

排序: 

sort() 函數用於對數組單元從低到高進行排序。
  rsort() 函數用於對數組單元從高到低進行排序。
  asort() 函數用於對數組單元從低到高進行排序並保持索引關係。
  arsort() 函數用於對數組單元從高到低進行排序並保持索引關係。
  ksort() 函數用於對數組單元按照鍵名從低到高進行排序。
  krsort() 函數用於對數組單元按照鍵名從高到低進行排序。

去重:

 array_unique() 函數移除數組中的重複的值,並返回結果數組。 

<?php $a=array("a"=>"Cat","b"=>"Dog","c"=>"Cat"); print_r(array_unique($a)); ?> 輸出: Array ( [a] => Cat [b] => Dog )

並集:array_merge();//並集

// $a=array("a"=>"Cat","b"=>"Dog","c"=>"Cat");
// $a1=array("a"=>"Cat2","b"=>"Dog3","c"=>"Cat4");
// $result=array_merge($a1,$a);
// print_r($result);

 

array_intersect()//交集

 

$a=array(1,2,3,6);$b=array(2,4,6,8);$result=array_intersect($b,$a);print_r($result);輸出:Array ( [0] => 2 [2] => 6 )

 array_diff() 求差集

$old = array(‘jpg‘,‘png‘,‘gif‘,‘bmp‘); $new = array(‘jpg‘,‘txt‘,‘docx‘,‘bmp‘); $difference = array_diff($old, $new); //在old數組中,不在new數組中print_r($difference);

數組轉字串

$arr = array(‘Hello‘,‘World!‘,‘I‘,‘love‘,‘Shanghai!‘);
echo implode(" ",$arr);

輸出  Hello World! I love Shanghai!

 

字串轉數組

$str="1432532 ,4444";
$arr = explode(",",$str);
print_r($arr);

輸出:Array ( [0] => 1432532 [1] => 4444 )

 

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.