Home Backend Development PHP Tutorial [Happy 100 days of learning PHP] Day two: Crazy array_PHP tutorial

[Happy 100 days of learning PHP] Day two: Crazy array_PHP tutorial

Jul 14, 2016 am 10:09 AM
Why sky open array of first day Link

Link to the previous issue: Happy 100 days of learning PHP, the first day

This issue’s motto:
Why do some people always feel that when learning PHP, they learn some knowledge points very well and others but never learn it? It is because the facial muscles are too tense when learning, which leads to necrosis of nerve endings, so they are lame.
Knowledge point in this issue: php array
Arrays are the most iconic tool function of PHP. If you learn PHP arrays well, you will basically have the initial capital to get involved in the PHP world.
I once had a friend who opened a computer company. Generally, the main business of computer companies is to sell computers, and occasionally provide some spare parts. Of course, depending on the situation, some companies also sell some CDs, such as movies, games, etc. My friend who is more technical is also a grassroots programmer, and he despises the act of selling CDs, especially CDs from island countries. For a long time in the early days, his main business was helping some companies or enterprises to build websites. Promotional websites at that time were not as complicated as they are now. They basically only had 2-3 pages. Dynamic websites would have no more than 10 interfaces. What’s more, there was a lot of free space at the time, so it was easy for him to earn hundreds of dollars at that time. , very agile and efficient. I once "stole" his website code. I can't remember all of it. I can only give a rough outline of it. You can take a look at it. PHP:
[php] view plaincopy
$var=file("./product list.txt");//It was better to use txt than access at that time
if(!$var || is_array($var) || count($var)==0) exit("The system is busy, please try again later");
$fix=array("China's largest XXX website", "Only our products are authentic", "Fake ones and fined ten for fakes will never cheat people", "Where can I buy such good XXX? Don't hesitate anymore "");
?>
<?php echo $fix[0]?></a>
//Note that 800*600 was the national standard at that time, don’t think too much


?

....This is a mess of fake Godly sentences...
....Here are advertisements interspersed by similar websites that support each other, such as: "Stir up the tiger in you" or "My legs and feet are healed after using XXX, but I can't afford it." It’s night” and so on.
echo '
  • '.$eachline.'
  • ';
    //The title of the product is very sensational, which means if you don’t buy it, you will regret your trip to this world.
    ?>
    ........Note that this is already the end of the page... //Note that the filing was really not strict at that time
                                                                         
    //Note that my friend did not know how to script at the time, so the page had to be refreshed once before the current time would change.
    Okay, the above is a basic skill that my friend relies on to survive. It is said that for a customer of the same type, he only needs to change the content of "Product List.txt" and then replace the background image of td, and the page will immediately get a new look. My friend told me very seriously at that time that he had achieved "productization" ” development model. I admire it so much because when I first learned ASP, it was definitely not so "configurable".
    Don’t be too entangled with the advertisements and statements in the webpage. Anyway, as a novice, I saw this webpage and wanted to spend money to give it a try, but my friend told me that I was not ready to use it yet. I asked "when can it be used", and my friend "slapped" me.
    Next, let’s get to the point and explain the above knowledge points.
    1. The most basic form of expression of array
    $fix=array("Content1","Content2","Content3"); This is the most basic way of expressing PHP arrays. Please forgive me for not typing the ad again, it's too disgusting.
    You can accumulate as much content as you want, as long as you can write it. When you want to call the content inside, you just need to start counting from "0", such as $fix[0], $fix[1]...$fix[n].
    Note: Why start from 0. One is because "php boss" designed it this way, and the other is because the true form of this most basic array is
    $fix=array(0=>"Content1",1=>"Content2",2=>"Content3");
    The symbol "=>" is omitted. The left side of this symbol is the key and the right side is the value. Generally, many textbooks will explain it as "$key=>$value". Don't worry about why the left side is $key. The right side is $value. I tell you that it is a customary way of writing. You have to write it as $ss=>$bb, which means that the one on the left is the key and the one on the right is the value.
    So: any form of array will have keys and values. Whether you omit it or not is up to you. Regardless of whether you omit it or not, I did it anyway.
    Expand a bit: Since there is a key value, 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 feel like vomiting after hearing this Advertising slogan"=>"One false penalty and ten penalty will never deceive people");
    If you want to output the "bullshit product brand" to the page at this time, you cannot use echo $fix[1]; because the key value has been changed by you.
    You should use echo $fix['Bullshit product brand'];
    2. Traverse the array
    Continue to use $fix=array("Content1", "Content2", "Content3"); as an example
    1. Using foreach is the most suitable and suitable method for looping small arrays.
    The basic syntax is: foreach (here is the original array as here is the variable set each time it is traversed)
    For example: foreach($var as $eachline) echo $eachline; will output content 1...content 3;
    2. Many people know that there is actually a while that can traverse an array
    The basic syntax is: while(list($key,$value)=each($attr))
    For example: while(list($key,$value)=each($fix)) echo $key.$value; will output 0 content 1. in sequence. . . . 2 Content 2;
    The difference between these two types of traversal will not be discussed too deeply here. It will be discussed later. I will only tell you now that if you just want to traverse data, then use foreach at any time. If you want to change the value of the array while iterating, use while. There is only one word for the reason, for "fast". Nowadays, the pace of life is too fast, and the first principle of writing programs is "fast".
    As for other syntaxes for traversing arrays, I personally think there is no need to learn them, unless you are going to take the exam. If it is actual combat, these two are enough, and we also want to be fast.
    To expand, the values ​​in the array can not only contain strings, but also arrays or any form of variable values.
    For example, $fix=array("Bullshit advertising slogans"=>array("The first 100 people who order will get another 200 yuan gift package", "Proficient in a certain language in 20 days", "Children don't eat because of lack of food" X"));
    For such an array, the value of $fix['Bullshit advertising words'] is actually an array,
    For example, echo $fix['Bullshit advertising slogan'][1]; will output "Master a language in 20 days"
    3. Assignment of arrays
    Let’s 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[]="Content2"; This is equivalent to $fix=array("Content1","Content2"); or $fix=array(0=>"Content1",1= >"Content 2");
    $fix['What are we learning']='php'; This is equivalent to $fix=array(0=>"Content 1","What are we learning"=>"php");
    The above assignments are all assigned at the end of the array. In fact, there is also the array_push function that can assign values. The syntax is $fix=array_push($fix,"Content1","Content2"); The effect is the same, except array_push You can add multiple values ​​at once, one at a time using '[]'.
    PHP array functions are very powerful. You can do almost anything you want to do, such as sorting, merging, reversing, deleting, etc. of arrays. You can search on Baidu. Due to space issues, I won’t go into details here. I’ll just use the functions back and forth. , not difficult. However, when it comes to actual projects, a lot of data processing needs to be done through database stored procedures, optimized table structures, good data sorting algorithms, and skilled data reading methods. In actual practice, many array functions in PHP are basically For example, if you receive a project like 1230X and want to list and sort the names of all Chinese people, do you dare to use a php array to traverse, merge, and reverse? Of course, if your customers are for the Vatican or Iceland, you can do this.
    However, there are many functions such as is_array--whether it is an array, in_array---whether a certain value exists, array_key_exists---whether a certain key value exists in the array, etc. Common functions must be learned. If you can't learn it, then you are not far away from becoming a leader.
    Easter egg:
    There is $var=file("./product list.txt"); above, which means that the text document is read at one time and read into an array line by line, including line breaks.

    www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477566.htmlTechArticle Previous issue link: Happy 100 days of learning PHP, the first day. This issue’s motto: Why do some people always feel good when learning PHP? I learned some knowledge points very well, but I still couldn't learn them. That's because my face was on my face when I was learning...
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    How to fine-tune deepseek locally How to fine-tune deepseek locally Feb 19, 2025 pm 05:21 PM

    Local fine-tuning of DeepSeek class models faces the challenge of insufficient computing resources and expertise. To address these challenges, the following strategies can be adopted: Model quantization: convert model parameters into low-precision integers, reducing memory footprint. Use smaller models: Select a pretrained model with smaller parameters for easier local fine-tuning. Data selection and preprocessing: Select high-quality data and perform appropriate preprocessing to avoid poor data quality affecting model effectiveness. Batch training: For large data sets, load data in batches for training to avoid memory overflow. Acceleration with GPU: Use independent graphics cards to accelerate the training process and shorten the training time.

    deepseek why can't you log in deepseek login portal deepseek why can't you log in deepseek login portal Feb 19, 2025 pm 05:00 PM

    There are a variety of reasons why DeepSeek cannot log in, including server failure, network connection issues, account disables, login credentials errors, or system updates. When you cannot enter the DeepSeek login portal, users can access the official website through https://www.deepseek.com/. If you encounter login problems, users should check the server status and troubleshoot network connection or login credential errors. If the problem persists, users should contact the DeepSeek support team for further assistance.

    Why can't the Bybit exchange link be directly downloaded and installed? Why can't the Bybit exchange link be directly downloaded and installed? Feb 21, 2025 pm 10:57 PM

    Why can’t the Bybit exchange link be directly downloaded and installed? Bybit is a cryptocurrency exchange that provides trading services to users. The exchange's mobile apps cannot be downloaded directly through AppStore or GooglePlay for the following reasons: 1. App Store policy restricts Apple and Google from having strict requirements on the types of applications allowed in the app store. Cryptocurrency exchange applications often do not meet these requirements because they involve financial services and require specific regulations and security standards. 2. Laws and regulations Compliance In many countries, activities related to cryptocurrency transactions are regulated or restricted. To comply with these regulations, Bybit Application can only be used through official websites or other authorized channels

    Free market software app website Free market software app website Mar 05, 2025 pm 09:03 PM

    This article introduces free digital asset quotation software apps and websites that can provide investors with key information such as real-time prices, price charts, transaction volume, fluctuations, market depth and news information to help investors make informed decisions. Compared with paid software, free software has the advantages of no cost, rich features, and easy operation. The article also guides users how to choose the right market software, and reminds users to pay attention to data sources, information accuracy and avoid excessive dependence, which ultimately helps investors better grasp the trends of the digital asset market. Want to know how to use free market software efficiently? Read the full text quickly!

    Why is Bittensor said to be the 'bitcoin' in the AI ​​track? Why is Bittensor said to be the 'bitcoin' in the AI ​​track? Mar 04, 2025 pm 04:06 PM

    Original title: Bittensor=AIBitcoin? Original author: S4mmyEth, Decentralized AI Research Original translation: zhouzhou, BlockBeats Editor's note: This article discusses Bittensor, a decentralized AI platform, hoping to break the monopoly of centralized AI companies through blockchain technology and promote an open and collaborative AI ecosystem. Bittensor adopts a subnet model that allows the emergence of different AI solutions and inspires innovation through TAO tokens. Although the AI ​​market is mature, Bittensor faces competitive risks and may be subject to other open source

    gateio exchange app old version gateio exchange app old version download channel gateio exchange app old version gateio exchange app old version download channel Mar 04, 2025 pm 11:36 PM

    Gateio Exchange app download channels for old versions, covering official, third-party application markets, forum communities and other channels. It also provides download precautions to help you easily obtain old versions and solve the problems of discomfort in using new versions or device compatibility.

    Bitcoin: The 'barometer' of global liquidity? Bitcoin: The 'barometer' of global liquidity? Mar 04, 2025 pm 06:39 PM

    The liquidity of the crypto market was tight, and Bitcoin fell again after continuing to fluctuate, hitting a low of $86,000, down nearly 20% from its peak. Bitcoin, the leader in cryptocurrency, has caused altcoins to collapse, and market sentiment has also shifted from fanaticism to panic. Is the Bitcoin bull market over? What is the future trend? These issues have attracted much attention. But Bitcoin trends are closely linked to global financial markets, and SwanBitcoin research analyst Sam Callahan's article on the relationship between Bitcoin price and global liquidity is worth reading. The core point of the article: Bitcoin price is consistent with global liquidity trends 83% of the time, which is higher than other major assets, making it an effective indicator for measuring liquidity conditions. Although Bitcoin is highly liquid in the world

    BitMEX: Best option strategy after a big sell-off BitMEX: Best option strategy after a big sell-off Mar 04, 2025 pm 06:27 PM

    Source: BitMEX Welcome to our weekly options Alpha series: Bitcoin has just dropped sharply, with a drop of 10%. Your portfolio is losing money and you may think, "What should you do next? Should you panic, increase your position, or do it smartly?" Options strategies can help manage risks, take advantage of possible rebounds, or generate revenue. Here are five effective strategies for post-selling scenarios, accompanied by examples, profit and loss analysis, and situational guidance. (As of this article was written, February 26, 2025, 13:06 Hong Kong time, the price of BTC was $88,584.) 1. If you are worried that Bitcoin will continue to decline in March... Practice: Buy protective put options (your insurance strategy) Assume you hold

    See all articles