PHP Seventh Lesson Array Usage 2

Source: Internet
Author: User
Tags foreach array arrays explode functions shuffle sort strlen

Learning outline:

1. Understanding Array Functions

2. Random Output Verification Code

1. Array function:



Array functions:
Role: Provides a lot of official writing of very useful code snippets, improve the speed of writing.

1. The key operation function of the array
2. Statistical elements and uniqueness of the array
3. Functions that use callback functions to handle arrays
4. Sorting function of array
5. Splitting, merging, decomposition and binding functions
6. Array and data structure
7. Other useful array-handling functions


The key-value action function of the array:
1.array_values ();

Simulation gets the value of key and value
 
  "User1", "Age" => "," "Sex" => "mans");


								foreach ($arr as $key => $val) {
									$keys []= $key;
									$vals []= $val;
								}
									
										echo "
";
										Print_r ($keys);
										echo "
"; echo " "; echo "
";
								Print_r ($vals);
								echo "
"; ? >




The use of 2.array_values
 
  "User1", "Age" => "," "Sex" => "mans");


					$keys =array_values ($arr);


					echo "
";
					Print_r ($keys);
					echo "
"; ? >



Array_values ()//Get the value in the array
Array_keys ()//Get the health in the array
In_array ()//check whether a value is in the array
Array_key_exists ()//Check whether a key is in the array
Array_flip ();//Key and value swap
Array_reverse (), inversion of values in array


Statistical elements and uniqueness of an array
1.count ();
2.array_count_values ();//counts the number of occurrences of each value in the array.
3.array_unique ()//delete duplicates in array


Functions that use callback functions to handle arrays:
1.array_filter ();
 
  70,60,80,78,34,34,34,56,78,78);


			function older ($var) {return
				($var >60);


			}


			$arr 2=array_filter ($arr, "older");
			
			echo "
";
			Print_r ($arr 2);
			echo "
"; ? >


2.array_map ();


Reference parameters:
Requirements: Array values from Add 1


Function Show (& $arr) {
foreach ($arr as $key => $val) {
$arr [$key]= $val +1;


}


}




Sorting Functions for arrays
1.sort (); Ascending, not preserving key
2.rsort (); Descending, key not preserved
3.asort (); Ascending, retaining key
4.arsort (); Descending, preserving key
5.ksort (); Sort Ascending according to key
6.krsort (); Sort Descending by key
7.natsort (); Sort ascending order of natural numbers, such as Picture img2.jpg
8.natcasesort (); Ignore upper-case ascending arrangement
9.multisort (); most group sorting




Ksort ();
 
  "B" =>1, "C" =>3, "D" =>30);


		$arr 2=array_flip ($arr);


		Ksort ($arr 2);


		echo "
";
		Print_r ($arr 2);
		echo "
"; ? >




Natsort ();
 
  






Most group ordering:
 
  ";
			Print_r ($arr 2);
			echo "
";?>





Splitting, merging, decomposition and binding functions
1.explode ();
2.inplode ();//join ();
3.array_slice (); Interception of arrays
4.array_splice (); Cropping of arrays
5.array-merge (); Merge most groups
6.array_combine (); Merge array, two array, previous array as key, and last array as value
7.array_intersect (); Find the intersection of two arrays
8.array_diff (); Find out the difference of two arrays, according to the first argument
9.array_pop (); Eject a value from the last and return the pop-up value
10.array_push (); Press a value from the last position to return the number of elements
11.array_shift () Remove a value from the position in front of the wash
12.array_unshift (); Pressing a value from the top position


 
  ";
			Print_r ($arr);
			echo "
";?>
2.inplode (); grouping arrays into strings
 
  ";
			Print_r ($str 2);
			echo "
";?>





 
  ";
				Print_r ($str 2);
				echo "
";?>




Array_slice ();
 
  ";
			     Print_r ($arr 3);
			     echo "
";?>
Not only is it removed, but it can be added


 
  ";
		    Print_r ($arr 2);
		    echo "
"; echo "
";
		    Print_r ($arr);
		    echo "
";?> Array_merge (); -->"; Print_r ($arr); echo "";?>





Other useful array-handling functions:
1.array_rand ()//randomly take a key
2.range ();//Remove a range of arrays
3.shuffle ();//Disturb the function of the array
4.array_sum ();//Calculate the and (calculate the total) of all the people in the array
If you calculate the sum of the keys of an array, you can use the Array_flip () to swap the weights and values of the arrays, and then you can calculate the sum of the kin.








 
  ";
    Print_r ($arr 2);
    echo "
";?>





Random output four-bit character verification code implementation:
 
  	


Reprint please indicate the source: HTTP://BLOG.CSDN.NET/JUNZAIVIP



Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.