[Happy Learning php100 Day] The next day: Crazy array
Previous link: Happy Learning php100 Day first day
The motto of this issue:
Why some people learn PHP always feel that some knowledge point learning is very good knowledge point but always learn not, it is because when the facial muscles are too tense, causing nerve endings necrosis, so lame.
knowledge Points: Php arrays
Arrays are the most iconic features of PHP. Learn PHP arrays Basically you have the initial capital mixed up in the PHP world.
I used to have a friend run a computer company. General Computer Company's main business is to sell computers, occasionally will take some spare parts, of course, depending on the situation there are some companies with the sale of some CDs, such as movies, games and so on. My friend is more technical. He is also a grassroots programmer, the sale of CD-ROM behavior is very despised, especially the CD-ROM to sell the island. At the beginning of a long period of time his main business is to help some companies or companies to do website. At that time, the propaganda site is completely not complex now, basically 2-3 pages, dynamic site more than 10 interface, and there is a lot of free space at that time, so he that hundreds of hundreds of earn very easy, very agile, very efficient ah, I have "steal teacher" his website code, I do not remember clearly all, Can only yy a bit about the appearance, we can underestimate, PHP:
<title><?php Echo $fix [0]?></title>
Attention at that time 800*600 is national standard, don't think too much
|
.... Here is a mess of the impracticality God sentence .... |
.... This is the same kind of Web site each other support interspersed advertising, such as: "Arouse your Heart of the Tiger" or "with xxx after the legs good, also not urinate" and so on. |
?
Echo ''. $eachline. ''; The title of the product is very scariness, meaning that you will regret a trip to the world without buying?> |
........ Note that this is already the end of the page ...//Note that the record was really lax //attention at that time my friend will not script, so the page will have to brush once to change the current time. |
Well, that's one of the basic skills that my friends rely on to survive. It is said to change the same type of customer he just put "product list. txt" exchange for content, and then the TD background picture replaced, the page immediately wash new leather noodles, my friend was very serious at that time told me that he has achieved "product" development model. I worship the pleasantly surprised, because I was a beginner ASP, there is absolutely no such "configurable."
Pages inside the ads and statements do not too tangled, anyway, at that time as a small white I read this page, I would like to spend money to try, but my friend told me that I did not use the time. I asked "When can I use", My friend "fan" me a slap.
Next, let's cut to the chase and explain the knowledge points above.
first, the most basic form of expression of an array
$fix =array ("Content 1", "Content 2", "Content 3"); This is the basic representation of the PHP array. Please forgive me for not wanting to repeat the advertising words again, it's disgusting.
You want to pile up as much content as you can, as long as you write it down. When you want to invoke the content inside, you simply call from "0" to start counting, such as $fix[0], $fix [1] ... $fix [n].
Note: Why start from 0. One is because "PHP boss" is so designed, the other is because the most basic array of the flesh is
$fix =array (0=> "Content 1",1=> "Content 2",2=> "Content 3");
"= =" This symbol is omitted, the symbol to the left is the key, the right is a value, in general, many textbooks will be interpreted as "$key and $value". Let's not tangle why the left is $key to the right is $value, I tell everyone is the customary wording, you have to write $ss > $bb, the one on the left is the key, and the one on the right is the value.
Therefore, any form of an array will have keys and values. The province does not omit to look at you, whether you save the province, anyway, I saved.
Expand: Since there is a key value, you can change the key value.
For example $fix =array ("exaggerated website name" and "China's biggest XXX website", "RIP egg product brand" = "Only we are the most authentic", "listen to want to vomit advertising language" = "false a penalty ten never deceptive");
This time if you want to export the "RIP product brand" to the page, you can't use echo $fix [1]; Because the key value has been changed by you.
Should use Echo to $fix [' RIP the Product brand '];
Second, iterate over the array
Continue to $fix =array ("Content 1", "Content 2", "Content 3"); For example
1. Using foreach is the most suitable method for looping small arrays.
The basic syntax is: foreach (here is the original array as the variable that is set at each traversal)
For example: The Echo $eachline of the foreach ($var as $eachline); This will output the content 1 .... Content 3;
2, many people know that there is actually a while can traverse the array
The basic syntax is: while (List ($key, $value) =each ($attr))
For example: while (List ($key, $value) =each ($fix)) echo $key. $value; The 0 contents of 1 ... will be output sequentially. 2 content 2;
These two traversal differences are not too deep here, and later, I'm just telling you, if you're just going to traverse the data, use foreach whenever you want. If you want to change the value of the array during traversal, use while. The reason is only one word, for "fast". Now the pace of life is too fast, the first principle of writing procedures is "fast."
As for the other syntax to iterate over an array, I personally think that we do not have to learn, unless you go to test the scholar, if the actual combat these two kinds of enough, we also for the fast.
Expand, the value of the array can not only put a string, you can also put an array can also be any form of variable values.
such as $fix =array ("the slogan" =>array ("The Top 100 subscribers, then send 200 yuan package", "20 days proficient in a language", "children do not eat because of lack of x"));
For such an array, the value of the $fix [' scrambled ad word '] is actually a group of numbers,
For example, Echo $fix [' The slogan '][1]; Will output "20 days of proficiency in a language"
iii. Assignment of arrays
To give an example:
$fix =array (); This array is empty.
$fix []= "Content 1"; This is equivalent to $fix =array ("Content 1") or $fix=array (0=> "Content 1");
$fix []= "Content 2"; This is equivalent to $fix =array ("Content 1", "Content 2"); or $fix =array (0=> "Content 1",1=> "Content 2");
$fix [' What we're learning ']= ' php '; This is equivalent to $fix =array (0=> "Content 1", "What we are learning" = "php");
The above assignment is in the end of the array assignment, in fact, there are array_push functions can be assigned, the syntax is $fix=array_push ($fix, "Content 1", "Content 2"), the effect is the same, but Array_push can add a lot of value, with ' [] ' One at a time.
PHP Array function is very powerful, almost want to do anything, such as array sorting, merging, reversing, delete and so on, we can Baidu a bit, because the length of the problem here not much to say, function backrest, rely on multi-use will, not difficult. However, to the actual combat to do the project a lot of data processing needs through the database stored procedures, optimized table structure, good data sorting algorithm, the technique of data read way to do, the actual actual combat PHP inside a good majority of the functions of the group is basically not used, for example, you received a 1230X such a project, To list all the Chinese names and sort you dare to use the PHP array to traverse the stem and merge and invert. Of course, if your client is facing the Vatican or Iceland, you can do that.
However, many functions, such as whether is_array--is an array in_array---whether there is a value, array_key_exists---the existence of a key value in the array, and so on the commonly used functions must be learned. If you do not learn, then you are not far from being a leader.
Egg:
There is a $var =file ("./Product list. txt") above; This means that the text document is read one time and the array is read by line, including newline characters.
The next day to speak PHP stalwart file operation, please warm up.