PHP-數組小記(二)

來源:互聯網
上載者:User

標籤:scl   函數名   ascll   post   參數   span   aaa   依次   count   

1.count()
 1 <?php 2     /*count()函數統計數組中元素個數*/   3     $student=array( 4         array(‘name‘=>‘wang‘,‘age‘=>18), 5         array(‘name‘=>‘lee‘,‘age‘=>20) 6         ); 7  8     echo count($student);//非遞迴2 9     echo count($student,1);//遞迴610 ?>

 

2.array_count_values()和array_unique()
1 <?php2     /*array_count_values()統計數組中值重複的次數,返回一個數組,鍵代表值名,值是重複次數*/3     $a=array(1,1,1,‘hello‘,‘hello‘,‘mike‘);4     print_r(array_count_values($a));//Array ( [1] => 3 [hello] => 2 [mike] => 1 ) 5 6 7     /*array_unique()刪除數組中重複的值,只保留第一次出現的鍵名*/8     print_r(array_unique($a));//Array ( [0] => 1 [3] => hello [5] => mike ) 9 ?>

 

3.array_filter()
 1 <?php 2     /*array_filter()傳入一個函數名字串,用該函數對數組進行過濾,返回true就保留,否則刪除*/  3  4     $a=array(1,2,3,4,5,6,7,8,9,10); 5  6     function myfun($i){ 7         if($i%2==0) return true; 8         else return false; 9     }10 11     print_r(array_filter($a,‘myfun‘));12 ?>

 

 4.array_walk()
 1 <?php 2     /*array_walk()*/ 3     /*第一個參數是一個數組的引用*/ 4     /*第二個參數是一個回呼函數,array_walk()依次將每一個值和鍵作為第一個第二個參數傳入*/ 5     /*如果回呼函數還有第三各參數,則可以在array_walk()第三個參數傳入*/ 6  7     function test($value,$key,$i) 8     { 9         echo "key: ".$key.$i.$value."<br>";10     }11 12     $a=array(1,2,3,4,5,6);13     array_walk($a,‘test‘,‘relation to value:‘);14     15 ?>

 

5.sort()和asort()
 1 <?php 2     /*sort()傳入一個數組的引用,將數組按值升序排列*/ 3      4     $a=array(3,7,9,1,2,3,10); 5     sort($a); 6     print_r($a);//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 3 [4] => 7 [5] => 9 [6] => 10 ) 7  8     /*如果是字母則按字母的ASCLL值升序排列*/ 9     $b=array(‘arr‘,‘cada‘,‘a‘,‘fsfafsj‘);10     sort($b);11     print_r($b);//Array ( [0] => a [1] => arr [2] => cada [3] => fsfafsj )12 13     /*rsort()函數與sort()相反  */14     rsort($a);15     print_r($a);//Array ( [0] => 10 [1] => 9 [2] => 7 [3] => 3 [4] => 3 [5] => 2 [6] => 1 )16 17     rsort($b);18     print_r($b);//Array ( [0] => fsfafsj [1] => cada [2] => arr [3] => a )19 20 21     /*asort()與sort()相似,但保留原始的索引值關係*/22 23     asort($a);print_r($a);//Array ( [6] => 1 [5] => 2 [4] => 3 [3] => 3 [2] => 7 [1] => 9 [0] => 10 ) 24 ?>

 

6.ksort()
1 <?php2     /*ksort()函數根據鍵對數組進行升序排列*/3     $a=array(3=>‘three‘,1=>‘one‘,7=>‘seven‘,2=>‘two‘);4     ksort($a);5     print_r($a);//Array ( [1] => one [2] => two [3] => three [7] => seven ) 6 ?>

 

7.自訂排序
 1 <?php 2     /*usort()函數可以傳入一個函數名字串,用該函數決定相片順序*/ 3  4     $a=array(‘asasas‘,‘asgsudg‘,‘asgsqyg‘,‘aaaaaaaaaaaa‘,‘sada‘); 5  6     function len($a,$b) 7     { 8         if(strlen($a)>strlen($b)) return 1; 9         else if(strlen($a)<strlen($b)) return -1;10         else return 0;11     }12     usort($a,‘len‘);13     print_r($a);14 15     /*同理也可以使用uasort()和uksort()*/16 ?>

 

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.