[Happy to learn php100 days] Day 2: crazy array

Source: Internet
Author: User
Link to the previous issue: Happy to learn php100day the first day of this article motto: Why do some people learn php always feel that some of the knowledge points are good, but some knowledge points are never learned, it is because the face muscles are too tight when learning, this results in peripheral nerve necrosis, so it is paralyzed. Knowledge Point: php array is p... "/> <scripttype =" text/javascript "src =" http: // www previous link: Happy to learn php100 day first day this motto: why do some people learn php and always feel that some knowledge points are well-learned and some knowledge points are never learned? it is because the facial muscles are too tight at the time of learning, resulting in nerve tip necrosis, so it is paralyzed. Knowledge Point: php array is the most iconic powerful tool of php. To learn the php array, you will have the initial capital in the php field. A friend of mine once started a computer company. Generally, computer companies sell computers and occasionally build some spare parts. of course, some companies sell CDs, such as movies and games, depending on the situation. My friend is also a grassroots programmer who is technically familiar with selling CDs, especially those from island countries. In the early stage, his main business was to help some companies or enterprises make websites. At that time, the publicity websites were not as complex as they were now. Basically, there were 2-3 pages, and there were no more than 10 Dynamic Websites. Besides, there was a lot of free space at that time, at that time, he earned several hundred or even a few hundred yuan easily, quickly, and efficiently. I used to "steal" his website code and I cannot remember it all, it can only look like yy. you can take a look at it. for php: [php] view plaincopy <? Php echo $ fix [0]?>


// Note that at that time, 800*600 is a national standard. do not think too much. ?
...... Here is a mess of fake big empty gods .......
...Mutual supportInterspersed advertisements, such as: "stir up the tiger in your heart" or "use XXX to get your legs and feet up and stay up late.
Echo'
  • '. $ Eachline .'
  • ';
    // The title of the product is an alarmist. if you don't buy it, you will regret it in the next life.>
    ... Note that the page is at the end of the page... // Note that my friend didn't script at the time, so the current time will be changed only after the page is refreshed once.
    Well, the above is a basic skill for my friends to survive. It is said that the customer of the same region only needs to change the product category .txt and the background image of td. the page is immediately updated. my friend told me very seriously that he has implemented the "productization" development mode. I worship it in five ways, because when I first started asp, I was absolutely not so "configurable ". Don't worry too much about the advertisements and statements on the web page. I saw this web page as a Tom and wanted to try it for a while, but my friend told me that I was not ready to use it. I asked "When can I use it?" and my friend slapped me. Next, let's start with the question and explain the knowledge points mentioned above. 1. the most basic representation of arrays $ fix = array ("content 1", "content 2", "Content 3"); this is the most basic representation of php arrays. Forgive me for not wanting to repeat the ad. You can accumulate as much content as you write. When you want to call the content, you only need to count the call from "0", such as $ fix [0], $ fix [1]... $ fix [n]. Note: Why is it starting from 0. One is because "php boss" is designed in this way, and the other is because the most basic array is $ fix = array (0 => "content 1 ", 1 => "content 2", 2 => "content 3"); "=>" This symbol is omitted. The symbol is a key on the left and a value on the right, in most textbooks, it is interpreted as "$ key => $ value ". don't worry about why $ key is on the left and $ value on the right. I tell you that it is a habitual writing. $ ss => $ bb represents the key on the left, the one on the right is the value. Therefore, an array of any form has keys and values. You can save it. Extended: since a key value exists, you can change the key value. For example, $ fix = array ("exaggerated website name" => "China's largest XXX website", "bullshit product brand" => "only we are the most authentic ", "Listening to the ad" => "fake, penalty, ten, never pitfall"). In this case, if you want to output the "fake product brand" to the page, you cannot use echo $ fix [1] Because the key value has been changed. Echo $ fix ['fake product brands'] should be used; 2. traverse the array to continue with $ fix = array ("content 1", "content 2 ", "Content 3"); for example 1, using foreach is the most suitable method for loop small arrays. The basic syntax is: foreach (here is the original array as here to write the variable set for each time) for example: foreach ($ var as $ eachline) echo $ eachline; the output content will be 1 ..... content 3; 2. many people know that there is also a while which 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; 0 content 1 ..... 2 Content 2; the differences between the two traversal methods are not discussed too deeply here. as we will talk about later, I will only tell you that if you just want to traverse data, you will use foreach whenever. If you want to change the value of the array during the traversal process, use while. The reason is only one word. Today, the pace of life is too fast. The first principle of writing a program is "fast". As for other syntax for traversing the array, I personally think that we do not need to learn it unless you go to test the talent. if it is practical, we also want to be fast. Extended, the values in the array can be put not only strings, but also arrays or variable values of any form. for example, $ fix = array ("bullshit ad word" => array ("The first 100 buyers, then send a 200 yuan gift bag", "20 days proficient in a language ", "The child does not eat because X is missing"); for such an array, $ fix the value of ['nonsense ads'] is actually an array, such as echo $ fix ['nonsense ads'] [1]; it outputs "20 days proficient in a language". 3. assign values to Arrays. for example, $ fix = array (). The 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 are learning '] = 'php '; in this case, it is equivalent to $ fix = array (0 => "content 1", "What we learn" => "php"). the above values are assigned at the end of the array, in fact, the array_push function can assign values. The syntax is $ fix = array_push ($ fix, "Content 1", "content 2"). The effect is the same, but array_push can add multiple values at once, use '[]' at a time. Php array functions are very powerful and can be used for almost everything you want, such as sorting, merging, reversing, and deleting arrays. you can refer to Baidu for details, the back-to-back function is not difficult. However, in practice, a lot of data processing needs to be done through database stored procedures, optimized table structures, good data sorting algorithms, and skillful data reading methods, in practice, php basically cannot use a lot of array functions. for example, if you have received a project like 1230X, to list the names of all Chinese people and sort them, do you dare to use the php array to traverse and merge and reverse them. Of course, if your customers are for the Vatican or Iceland, you can do that. However, many functions, such as is_array -- whether the array is in_array -- whether a value exists, and array_key_exists -- whether a key value exists in the array, must be learned. If you cannot learn it, you are not far from being a leader. Egg: $ var = file ("./product release .txt") is included in the preceding section. This means that the text document is read at one time and the text document is read as an array by row, including line breaks.

    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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.