php-An array of small notes (ii)

Source: Internet
Author: User

1.count ()
1<?PHP2     /*count () function counts the number of elements in an array*/  3     $student=Array(4         Array(' name ' = ' Wang ', ' age ' =>18),5         Array(' name ' = ' Lee ', ' age ' =>20)6         );7 8     Echo Count($student);//non-recursive 29     Echo Count($student, 1);//Recursive 6Ten?>

2.array_count_values () and Array_unique ()
1<?PHP2     /*array_count_values () counts the number of repetitions in the array, returns an array, the key represents the value name, and the value is the number of repetitions*/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 () removes duplicate values from the array, preserving only the first occurrence of the key name*/8     Print_r(Array_unique($a));//Array ([0] = 1 [3] = = Hello [5] = Mike)9?>

3.array_filter ()
1<?PHP2     /*Array_filter () passes in a function name string, which is filtered by an array of functions, returns True to be preserved, otherwise deleted*/ 3 4     $a=Array(1,2,3,4,5,6,7,8,9,10);5 6     functionMyfun ($i){7         if($i%2==0)return true;8         Else return false;9     }Ten  One     Print_r(Array_filter($a, ' Myfun ')); A?>

4.array_walk ()
1<?PHP2     /*Array_walk ()*/3     /*The first argument is a reference to an array*/4     /*The second parameter is a callback function, in which array_walk () sequentially passes each value and key as the first second parameter in a*/5     /*if the callback function also has a third parameter, you can pass in the third parameter of the Array_walk ()*/6 7     functionTest$value,$key,$i)8     {9         Echo"Key:".$key.$i.$value." <br> ";Ten     } One  A     $a=Array(1,2,3,4,5,6); -     Array_walk($a, ' Test ', ' relation to value: '); -      the?>

5.sort () and Asort ()
1<?PHP2     /*sort () passes in a reference to an array, sorting the array in ascending order of values*/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] = +)7 8     /*If the letter is in ascending order by the ASCLL value of the letter*/9     $b=Array(' arr ', ' cada ', ' a ', ' FSFAFSJ ');Ten     Sort($b); One     Print_r($b);//Array ([0] = a [1] = arr [2] = = Cada [3] = FSFAFSJ) A  -     /*the Rsort () function is opposite to sort ()*/ -     Rsort($a); the     Print_r($a);//Array ([0] = [1] = 9 [2] = 7 [3] = 3 [4] = 3 [5] = 2 [6] = 1) -  -     Rsort($b); -     Print_r($b);//Array ([0] = FSFAFSJ [1] = Cada [2] = arr [3] = = a) +  -  +     /*Asort () is similar to sort (), but retains the original key-value relationship*/ A  at     Asort($a);Print_r($a);//Array ([6] = 1 [5] = 2 [4] = 3 [3] = 3 [2] = 7 [1] = 9 [0] = +) -?>

6.ksort ()
1 <? PHP 2     /* the Ksort () function is arranged in ascending order based on the key array */ 3     $a=array(3=> ' three ',1=> ' one ',7=> ' seven ',2=> ' both '); 4     Ksort ($a); 5     Print_r ($a); //  6 ?>

7. Custom sorting
1<?PHP2     /*the Usort () function can pass in a function name string, which determines how it is arranged*/3 4     $a=Array(' Asasas ', ' asgsudg ', ' asgsqyg ', ' aaaaaaaaaaaa ', ' Sada ');5 6     functionLen$a,$b)7     {8         if(strlen($a) >strlen($b))return1;9         Else if(strlen($a) <strlen($b))return-1;Ten         Else return0; One     } A     Usort($a, ' Len '); -     Print_r($a); -  the     /*Similarly, you can also use Uasort () and Uksort ()*/ -?>

php-An array of small notes (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.