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

Source: Internet
Author: User
Tags php foreach
[Happy to learn php100 days] Day 2: crazy array. Previous link: Happy to learn php100day first day this article motto: Why do some people learn php always feel that some knowledge points are well learned, but some knowledge points are never learned, it is because when learning face previous link: happy to learn php100day first day

Current 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.

Current knowledge point: php array

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, you can only look like yy. you can look down at php:

[Php] view plaincopy

$ Var = file ("./product example .txt"); // txt is not recommended for access at the time.

If (! $ Var | is_array ($ var) | count ($ var) = 0) exit ("The system is busy, please try again later ");

$ Fix = array ("XXX's largest website in China", "only our products are authentic", "a penalty of ten false positives will never be pitfall ", "Where can I buy such a good XXX? don't hesitate ");

?>

<? 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.

    ?>

    ... 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.

    I. basic representation of arrays

    $ Fix = array ("content 1", "content 2", "Content 3"); this is the most basic expression 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. it is a key on the left and a value on the right. generally, it is interpreted as "$ key => $ value" in many textbooks ". 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 ", "I have heard the advertisement that I want to vomit" => "fake, one penalty, Ten will never be 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;

    II. Traverse arrays

    Continue with the example of $ fix = array ("content 1", "content 2", "Content 3 ");

    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 1 ...... content 3;

    2. many people know that there is a while that can traverse the array.

    Basic syntax: while (list ($ key, $ value) = each ($ attr ))

    For example, while (list ($ key, $ value) = each ($ fix) echo $ key. $ value; 0 content 1 is output in sequence ..... 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 only want to traverse data, you can 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 ", "Children do not eat because X is missing "));

    For such an array, the value of $ fix ['nonsense ads'] is actually an array,

    For example, echo $ fix ['nonsense ads'] [1] will output "20 days proficient in a language"

    3. array assignment

    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'; this is equivalent to $ fix = array (0 => "content 1 ", "What are we learning" => "php ");

    The preceding 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, except that array_push can add multiple values at once and use.

    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:

    The preceding $ var = file ("./product example .txt") refers to reading text documents at one time and reading them into an array by row, including line breaks.

    The first day of this article's motto: Why does some people learn php and always feel that some of the knowledge points are well learned, but some are never learned? that's because of the face when learning...

    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.