Home > php教程 > php手册 > PHP summary of the first article (I hope you can add more! Thank you)

PHP summary of the first article (I hope you can add more! Thank you)

WBOY
Release: 2016-11-16 10:24:01
Original
1443 people have browsed it

/* Commonly used functions for arrays
*
* Sorting functions for arrays
* sort()
* rsort()
* usort()
* asort()
* arsort()
* uasort()
* ksort()
* krsort()
* uksort()
* uatsort()
* natcasesort()
* array_multisort()
*
* 1. Simple array sorting
* sort() rsort()
* 2. According to key name Sort the array
*   ksort() krsort()
* 3. Sort the array according to the value of the element
*   asort() arsort()
* 4. Sort the array according to the "natural number sorting" method
*   natsort()// Comparison of uppercase and lowercase letters natcasescort()//Comparison of insensitive letters
* 5. Sort the array according to user-defined rules
*   usort() uasort() uksort() Sort the keys
* 6. Sort the dimensional array Sorting
* ​ array_multisort()
*
* Array functions for splitting, merging, decomposing, and joining
* 1. array_slice()
* 2. array_splice()//Delete
* 3. array_combine()//Merge
* 4.array_merge();//Merge
* 5.array_intersect();//Intersection of multiple arrays
* 6.array_diff();//Return the difference of multiple arrays
*
* Arrays and data structures Functions
* 1. Use arrays to implement stacks//first in, last out
* *
* Other functions related to array operations
* * array_rand()
* shuffle()
* array_sum()
* range()
*/

//The use of simple array sorting
$data=array(5,8,1,7,2);
sort($data);//Sort the elements from small to large

print_r($data);// Array ( [0] => 1 [1] => 2 [2] => 5 [3] => 7 [4] => 8 )

rsort($data);//Elements are sorted by Sort to smallest
print_r($data);//Array ( [0] => 8 [1] => 7 [2] => 5 [3] => 2 [4] => 1 )

//Example of sorting based on key name
$data_2=array(5=>"five",8=>"eight",1=>"one",7=>"seven",2=> ;"two");
ksort($data_2);//Sort the subscripts of the array from small to large

print_r($data_2);//Array ( [1] => one [2] => two [5] => five [7] => seven [8] => eight )

krsort($data_2);//Sort the subscripts of the array from large to small
print_r($data_2); //Array ( [8] => eight [7] => seven [5] => five [2] => two [1] => one )



//Sort the array according to the value of the element

$data_3=array("1"=>"Linux","a"=>"Apache","m"=>"MySQL","l"= >"PHP");

//asort() arsort The difference between sort() rsort() is that the former keeps the original key names after sorting, while the latter does not keep the original key names, and the key names start from 0

asort($data_3);

print_r($data_3);//Array ( [a] => Apache [1] => Linux [m] => MySQL [l] => PHP )
echo '
';
arsort($data_3);
print_r($data_3);//Array ( [l] => PHP [m] => MySQL [1] => Linux [a] => Apache )
echo '
';
sort($data_3);
print_r($data_3);//Array ( [0] => Apache [1] => Linux [2] => MySQL [3] => PHP )
echo '
';
rsort($data_3);
print_r($data_3);//Array ( [0] => PHP [1] => MySQL [2] => Linux [3] => Apache )

//Sort the array according to the "natural number sorting method" (0-9 shorter ones are given priority)
$data_4=array("file.txt","file11.txt","file2.txt","file22.txt") ;
sort($data_4);

print_r($data_4);//Array ( [0] => file.txt [1] => file11.txt [2] => file2.txt [3] = > file22.txt )

echo '
';
natsort($data_4);
print_r($data_4);//Array ( [0] => file.txt [2] => file2.txt [1 ] => file11.txt [3] => file22.txt )
echo '
';
natcasesort($data_4);
print_r($data_4);//Array ( [0] => file.txt [2] => file2.txt [1] => file11.txt [3] => file22.txt )
echo '
';

//User-defined sorting function
echo '
';
$data_5=array("Linux","Apache","MySQL","PHP");
usort($data_5,"sortbylen");// Sort by element length
print_r($data_5);//Array ( [0] => PHP [1] => MySQL [2] => Linux [3] => Apache )
function sortbylen($one ,$two){
if(strlen($one)==strlen($two))
return 0;
else
return (strlen($one)>strlen($two))?1:-1;
}

//Array functions for splitting, merging, decomposing and joining
echo '
';
$data_6=array("Linux","Apache","MySQL","PHP");
print_r(array_slice($data_6 ,1,2));//Remove the elements with subscripts 1 and 2
//Array ( [0] => Apache [1] => MySQL ) The subscript is reset from 0
echo '
' ;

print_r(array_slice($data_6,-2,1));//Get back one starting from the second one, not starting from 0
//Array ( [0] => MySQL ) subscript reset Starting from 0
echo '
';

print_r(array_slice($data_6,1,2,true));
//Array ( [1] => Apache [2] => MySQL ) Retain the original subscript

echo '
';


//array_combine()
$a1=array("OS","WebServer","DataBase","Language");
$a2=array("Linux","Apache","MySQL","PHP ");

print_r(array_combine($a1,$a2));//The first parameter is used as the key name, and the second parameter is used as the value to merge
//Array ( [OS] => Linux [WebServer] => Apache [ DataBase] => MySQL [Language] => PHP )

echo '
';

//array_merge()
$a3=array("OS","WebServer","DataBase","Language");
$a4=array("Linux","Apache","MySQL","PHP" );
$a5=$a3+$a4;
print_r($a5);//Because the two array subscripts overlap, it is displayed like this
//Array ( [0] => OS [1] => WebServer [ 2] => DataBase [3] => Language )
echo '
';
print_r(array_merge($a3,$a4));//Merge and reindex
//Array ( [0] => OS [1] => WebServer [2] => DataBase [3] => Language [4] => Linux [5] => Apache [6] => MySQL [7] => PHP )

echo '
';

//array_intersect()
$a7=array("OS","WebServer","DataBase","Language",1,2,3);
$a8=array("Linux","Apache"," MySQL","PHP",2,3,4);
print_r(array_intersect($a7,$a8));//Array ( [5] => 2 [6] => 3 )
echo '
';

//array_diff()
$a9=array(1,2,3,4);
$a10=array(3,4,5,6);
print_r(array_diff($a9,$a10));/ /Array ( [0] => 1 [1] => 2 )
//Return the difference between the first array and the second element
echo '
';


//Use array to implement stack
$b=array(1,2,3,4);
$b[]="a";//Push to stack
array_push($b,"b","c" );//Use function to push onto the stack
print_r($b);//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => a [5] => b [6] => c )
echo '
';

$value=array_pop($b);//Use function to pop
print_r($b);//Array ( [0] => 1 [1] => 2 [2] => 3 [3 ] => 4 [4] => a [5] => b )
echo '
';
echo $value;//Display the value of the element popped off the stack c
echo '
';

//Use array to implement queue
$c=array(1,2,3);
print_r($c);//Array ( [0] => 1 [1] => 2 [2] => ; 3)
echo '
';
array_unshift($c,"abc","bcd");//Enqueue
print_r($c);//Array ( [0] => abc [1] = > bcd [2] => 1 [3] => 2 [4] => 3 )
echo '
';
$values=array_shift($c);//dequeue
print_r($c );// Array ( [0] => bcd [1] => 1 [2] => 2 [3] => 3 )
echo '
';
unset($c[2]) ;//Delete the element at the specified position
print_r($c);//Array ( [0] => bcd [1] => 1 [3] => 3 )
echo '
';


//array_rand() Randomly returns the array subscript
$arr=array(1,3,4,5,76,7,99,6,2,3);
echo array_rand($arr);//return is the subscript of a random array element
echo $arr[array_rand($arr)];//Randomly displays the value of the array element
echo '
';
//shuffle() Randomly rearranges the array
$arr2=array (32,35,33);
shuffle($arr2);
print_r($arr2);//Array element position is randomly changed
echo '
';
//array_sum() Sum
$arr3=array(1 ,3,5);
echo array_sum($arr3); //Return 9
echo '
';
print_r($arr3);//Array ( [0] => 1 [1] => 3 [ 2] => 5 )
echo '
';
//range(minimum value, maximum value, step size)
$arr4=range(0,100,10);
print_r($arr4);//Array ( [ 0] => 0 [1] => 10 [2] => 20 [3] => 30 [4] => 40 [5] => 50 [6] => 60 [7 ] => 70 [8] => 80 [9] => 90 [10] => 100 )

?>

Function to determine type:::::::

  1. is_bool() //Determine whether it is a Boolean type
  2. is_float() //Determine whether it is a floating point type
  3. is_real() //Same as above
  4. is_int() //Determine whether it is an integer type
  5. is_integer() //Same as above
  6. is_string() //Determine whether it is a string
  7. is_object() //Determine whether it is an object
  8. is_array() //Determine whether it is an array
  9. is_null() //Determine whether it is null
  10. is_file() //Determine whether it is a file
  11. is_dir() //Determine whether it is a directory
  12. is_numeric() //Determine whether it is a number
  13. is_nan()​​​​​​//Judge if it is not a number​​​​​​​​ is_resource()
  14. //Determine whether it is a resource type
  15. is_a(
  16. $obj,
  17. $classname) //Determine whether the object is an instance of the class

str_type function str_getcsv(

$str);
    //Convert csv file string into an array
  1. str_replace(
  2. $search,
  3. $replace,$subject [,&$count]);//Search and replace string 个 四 // If the fourth parameter is specified, the number of times the replacement will be assigned to him
  4. str_ireplace($search,$replace,
  5. $subject [,&
  6. $count]);//Search and replace string 个 四 // If the fourth parameter is specified, the number of times he will be assigned to him is ignored. str_shuffle(string $str);//Shuffle the string randomly
  7. str_split($str [,$len=1]);//Convert the string into an array
  8. 个 每 //, the length of each array unit is $ len Convert string type:
strval(

$str);

//Convert to string type

floatval(
    $str);
  1. //Convert to floating point type intval(
  2. $str);
  3. //Convert to integer type
  4. Convert case
    1. strtolower($str); //Convert all to lowercase
    2. strtoupper($str); //Convert all to uppercase

    Function to remove html tags

    1. strip_tags($str [,$tags]);//Remove all tags except the tags in $tags

    ASCII Convert Number Array Convert ASCII

    1. chr(int $ascii); //Convert numbers to ascii
    2. ord(string $str); //Return the ascii value of the first character of $str

    Remove spaces

     trim(string $str [,string $charlist ]); //Remove left and right characters

    1. trim(string $str [,string $charlist ]); //Remove left and right characters
    2. ltrim(string $str [,string $charlist ]); //Remove left characters
    3. rtrim(string $str [,string $charlist ]); //Remove right characters

    Magic method

     __construct

     __destruct

     __set

     __unset

     __isset

     __get

     __debuginfo

     __invoke

     __call

     __sleep

     __wakeup

     __clone

     __toString

    Common design patterns

    Single instance factory injection observer strategy factory method adapter etc;;;;

    Commonly used databases:

    ORACLE; MySQL; SQLServer; etc.

    This is just a rough summary. That’s all I can think of for now. I hope it can help some beginners! !

    Suitable for beginners. If you need it, please mention it in the comments!

    Will be updated in the future! ! ! ! ! ! ! ! ! ! !

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template