Php array -- load server files into an array and display it as a web page. First, the server has an orders.txt file.
Load the file into an array and display it. create vieworder2.php
Customer orders Orders from our store customers
No order information. please try again!";} Echo"
Order Date |
Men's wear |
Shoes |
Glasses |
Total |
Shipping Address |
"; For ($ I = 0; $ I <$ number_of_orders; $ I ++) {// splits elements by space $ line = explode (" \ t ", $ orders [$ I]); // converts a string to an integer $ line [1] = intval ($ line [1]); $ line [2] = intval ($ line [2]); $ line [3] = intval ($ line [3]); echo"
$ Line [0] |
$ Line [1] |
$ Line [2] |
$ Line [3] |
$ Line [4] |
$ Line [5] |
";} Echo"
";?>The last displayed page is:
Additional knowledge points:
'; For ($ I = 0; $ I <26; $ I ++) {// use for loop echo $ letters [$ I]. "";} echo'
'; Foreach ($ odds as $ current) {// use foreach echo $ current. "";} // initialize the associated array $ prices = array ('cloths' => 150, 'shoes' => 300, 'windows' => 35 ); // use the loop statement to Access the associated array. echo'
'; Foreach ($ prices as $ key => $ value) {// use foreach echo $ key.' _ '. $ value .'
';} Reset ($ prices); // use the reset function to reset the current element to the beginning of the array while ($ element = each ($ prices )) {// use the each function echo $ element ['key']. '_'. $ element ['value'].'
';} Reset ($ prices); while (list ($ product, $ price) = each ($ prices) {// use the list function echo "$ product-$ price
";}// Create a two-dimensional array $ products2 = array ('cloth ', 'cloths', 150), array ('shoe', 'shoes', 300 ), array ('glass ', 'Glass', 35); // use double for to access the array element for ($ row = 0; $ row <3; $ row ++) {for ($ column = 0; $ column <3; $ column ++) {echo '| '. $ products2 [$ row] [$ column];} echo '|
';} // Create a two-dimensional join array $ products3 = array ('code' => 'br', 'description' => 'cloths ', 'price' => 150), array ('code' => 'shoe', 'description' => 'shoes', 'price' => 300 ), array ('code' => 'glass ', 'description' => 'glas', 'price' => 35 )); // use the for loop to access the two-dimensional associated array for ($ row = 0; $ row <3; $ row ++) {echo '| '. $ products3 [$ row] ['code']. '| '. $ products3 [$ row] ['description']. '| '. $ products3 [$ row] ['price']. '|
';} // Use the each function and list function to access reset ($ products3); for ($ row = 0; $ row <3; $ row ++) {while (list ($ key, $ value) = each ($ products3 [$ row]) {echo "| $ value";} echo '|
';} /**************************** // Use the sort function to sort by letter sort in ascending order $ products4 = array ('cloths ', 'shoes', 'glass'); sort ($ products4); // sort by number in ascending order using the sort function $ prices3 = array (150,300, 35 ); sort ($ prices3); // use the asort function to sort the values of each element in the array. $ prices4 = array ('cloths' => 150, 'shoes' => 300, 'Glass' => 35); asort ($ prices4); // Use The ksort function to sort ksort ($ prices4) based on the array keyword ); // In addition, the corresponding reverse sorting methods include rsort (), arsort (), and krsort (); // user-defined sorting: usort () $ products5 = array ('cloth ', 'cloths', 150), array ('shoe', 'shoes', 300), array ('glass ', 'Glass', 35); function compare ($ x, $ y) {if ($ x [1] = $ y [1]) {return 0 ;} else if ($ x [1] <$ y [1]) {return-1 ;}else {return 1 ;}} usort ($ products5, 'Company '); // re-sort the array: random sort shuffle (), reverse sort array_reverse () $ numbers = range (); // $ numbers = array_reverse ($ numbers ); $ numbers = shuffle ($ numbers); for ($ I = 0; $ I <3; $ I ++) {// use the for loop echo $ numbers [$ I]. "";}
Supplement common functions of arrays
Current ($ array_name )?? Returns the first element;
Next ($ array_name )?? Move the pointer forward, and then return the new current element;
Each ($ array_name )?? Returns the current element before the pointer moves forward;
Reset ($ array_name )?? Returns the pointer to the first element of the array;
End ($ array_name )?? Move the pointer to the end of the array;
Prev ($ array_name )?? Move the current pointer back and return the new current element, which is opposite to next;
Array_walk ()?? Apply any function to each element of the array;
Count (), sizeof (), array_count_values ()?? Count the number of array elements;
Extract ()?? Create a series of scalar variables using an array;
For more information about arrays, see The php manual _ array
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.