Chapter 4 data processing php array processing aki cheng

  • 2020-05-09 18:19:29
  • OfStack

1. Array processing:
1.1 array creation and initialization:
1. The arrary() function creates an array. By default, the 0 element is the first element of the array.
The count() and sizeof() functions get the number of data elements
2. Use variables to create arrays
compact() looks up the variable name in the current symbol table and adds it to the output array, where the variable name becomes the key name and the contents of the variable become the key value.
 
<?php 
$num=10; 
$str="string"; 
$array=array(1,2,3); 
$newarray=compact("num","str","array"); 
print_r($newarray); 
/* The results of  
array([num]=10 [str]=>string [array]=>array([0]=>1 [1]=>2 [2]=>3)) 
*/ 
?> 

extract() converts the elements in the array into variables
 
<?php 
$array=array("key1"=>1,"key2"=2,"key3"=3); 
extract($array); 
echo "$key1 $key2 $key3";// The output 1 2 3 
?> 

3. Create an array using two arrays
 
array_combine(array $keys, array $values) 
<?php 
$a=array('green','red','yellow'); 
$b=array(' volcado','apple','banana'); 
$c=array_combine($a,$b); 
print_r($c); 
?> 

4. Create an array of specified ranges
range()
5. Automatically create an array
1.2 operations on key names and values
This section covers only the commonly used ones
. Check whether the array has a certain key name and value to use. The array_key_exists() and in_arrary functions, isset() check the key names in the array, isset() returns false when the key is NULL, array_key_exists() returns true.
. The array_search() function checks if the array's key value exists and returns NULL if it does not.
. The key() function gets the key name of the current cell in the array.
. The list() function assigns an array value to a specified variable. Very useful in array traversal.
< ?php
$arr=array(" red "," blue "," white ");
list($red,$blue,$white)=$arr;
echo $red; / / red
echo $blue; / / blue
echo $white; / / white
. array_fill() and array_fill_keys() can populate an array of values and key names with a given watch
. array_filp() can swap the key names and values in the array, and if the same value exists in the swap array, the same value is converted to the key name, and the last value is retained
. The array_keys() and array_values() functions take the keys and values from the array and store them in a new array.
. array_splice(arry $input,int $offset[,int $length[,array $replacement]]) removes one or more cells from the array and replaces them with other values.
. array_unique(), which removes duplicate values from an array and returns a new array without breaking the original array.
1.3 array traversal and output
1. Loop through the array using while
The array is traversed using the while, list(), each() functions
2. for loops through the array
3. Loop through the array using foreach
 
<?php 
$color=array("a"=>"red","blue","white"); 
foreach($color as $value) 
{ 
echo $value."<br>";// Output the value of the array  
} 
foreach($color as $key=>$value) 
{ 
echo $key."=>".$value."<br>";// Outputs the array's key name and value  
} 
?> 

Example 4.1 on the page, a text box is generated, the user enters the student's score, submits the form and outputs the value of the score less than 60, and calculates the average score.
 
<?php 
echo "<form method=post>"; // The new form  
for($i=1;$i<6;$i++) // Loop to generate the text box  
{ 
// The name of the text box is the array name  
echo " students ".$i." The achievement of :<input type=text name='stu[]' ><br>"; 
} 
echo "<input type=submit name=bt value=' submit '>"; // The submit button  
echo "</form>"; 
if(isset($_POST['bt'])) // Check that the submit button is pressed  
{ 
$sum=0; // The total score is initialized to 0 
$k=0; 
$stu=$_POST['stu']; // Gets the values of all the text boxes and assigns them to an array $stu 
$num=count($stu); // Calculate the array $stu Number of elements  
echo " The grades you enter are: <br>"; 
foreach($stu as $score) // use foreach Loop through groups $stu 
{ 
echo $score."<br>"; // Output the received value  
$sum=$sum+$score; // Calculate the total score  
if($score<60) // Judgment score less than 60 In the case  
{ 
$sco[$k]=$score; // Make the score less than 60 Is assigned to the array $sco 
$k++; // An array of $sco Key name index plus 1 
} 
} 
echo "<br> below 60 Score: <br>"; 
for($k=0;$k<count($sco);$k++) // use for Loop output $sco An array of  
echo $sco[$k]."<br>"; 
$average=$sum/$num; // Calculate the average score  
echo "<br> Average score: $average"; // Output average  
} 
?> 

1.4 sorting of arrays
1. Ascending sorting. sort (array $array [, int $sort_flags])
Note: keep sorting with mixed type values to a minimum, as errors may occur.
asort() can also be sorted in ascending order. It sorts the values of an array, but its sorted array retains the association between the keys and the values.
Ksort() sorts the array's key names, and the association between the key names and the values does not change after sorting.
2. Sort in descending order. rsort(), arsort(), krsort()
3. Sorting multi-dimensional array.
4. Reorder the array.
. The shuffle() function arranges the array in a random order and deletes the original key names
. array_reverse () function. Sorts an array in reverse order.
5. Natural ordering
. natsort(). Case sensitive
1.5 other operations
1. Merge the array
array_merge($array1,$array2). After the merge, all the arrays after 1 dimension are returned as one cell. array_merge_recusive() can merge arrays while maintaining the existing array structure.
2. Stack operation of array.
The stack: array_pop ($arr);
Stack: array_push ($arr, var);
3. Get the current cell of the array
1. The current() function can get the value of the cell pointed to by the internal pointer of the array, but it does not move the internal pointer of the array.
2. next($arr), move the pointer to the next cell.
3. end($arr) moves the pointer to the tail.
4. Array calculation
count(), sizeof() calculate the number of elements in the array
The array_count_values() function calculates the number of occurrences of a value in an array
Example :4.2 processing of tabular data
Receive user input information about students' learning events, names, grades, etc., and store the received information into an array and sort them in ascending order according to their grades. Then output it in a table. .
 
<form name=fr1 method=post> 
<table align=center border=1 > 
<tr> 
<td><div align=center> Student id </div></td> 
<td><div align=center> The name </div></td> 
<td><div align=center> results </div></td> 
</tr> 
<?php 
for($i=0;$i<5;$i++) // Loop to generate the text box of the table  
{?> 
<tr> 
<td><input type=text name="XH[]"></td> 
<td><input type=text name="XM[]"></td> 
<td><input type=text name="CJ[]"></td> 
</tr> 
<?}?> 
<tr><td align ="center" colspan="3"> 
<input type="submit" name="bt_stu" value=" submit "></td></tr> 
</table> 
</form> 
<center><font size=3 color="red"> 
 Note: student number values cannot be repeated </font></center><br> 
<!--  So that's the input form  --> 
<?php 
if(isset($_POST['bt_stu'])) // Determine if the button is pressed  
{ 
$XH=$_POST['XH']; // Receive the values of all student Numbers into an array $XH 
$XM=$_POST['XM']; // Receive the values of all names into an array $XM 
$CJ=$_POST['CJ']; // Receive the values of all grades into an array $CJ 
array_multisort($CJ,$XH,$XM); // To the above 3 Array sort, $CJ Is the primary array  
for($i=0;$i<count($XH);$i++) 
$sum[$i]=array($XH[$i],$XM[$i],$CJ[$i]); // will 3 An array of values 1 a 2 Dimensional array $sum 
echo "<div align=center> The ranking table is as follows :</div>"; 
// Head of form  
echo "<table align=center border=2><tr><td> Student id </td><td> The name </td><td> results </td></tr>"; 
foreach($sum as $value) // use foreach Loop through groups $sum 
{ 
list($stu_number,$stu_name,$stu_score)=$value; // use list() Function assigns the value of an array to a variable  
// Output table contents  
echo "<tr><td>$stu_number</td><td>$stu_name</td><td>$stu_score</td></tr>"; 
} 
echo "</table><br>"; // Form the tail  
reset($sum); // reset $sum Pointer to array  
while(list($key,$value)=each($sum)) // use while Loop through groups  
{ 
list($stu_number,$stu_name,$stu_score)=$value; 
if($stu_number=="081101") // Query whether there is a student number of 081101 The value of the  
{ 
echo "<center><font size=4 color=red>"; 
echo $stu_number." Name is: ".$stu_name." . "; 
echo " Results as follows: ".$stu_score; 
break; // Find and end the loop  
} 
} 
} 
?> 

Related articles: