[愉快学php100天]第二天:疯狂的数组
[开心学php100天]第二天:疯狂的数组
上期链接:开心学php100天
第一天
本期格言:
为什么有的人学php总感觉有的知识点学的很好有的知识点却始终学不会,那是因为学的时候脸部肌肉太紧张,导致神经末梢坏死,所以瘸了。
本期知识点:php数组
数组是php最为标志性的利器功能。学好php数组基本上你就有了在php界混的初期资本了。
以前我有一个朋友开了一个电脑公司。一般电脑公司主营业务就是卖电脑,偶尔也会搭一些零配件,当然视情况不同也有一些公司附带着卖一些光盘,如电影、游戏等。我那朋友比较有技术气息自己也是草根程序员出身,对卖光盘行为很鄙视尤其是卖岛国的光盘。初期有一大段时间他的主营业务是帮一些公司或企业做网站。当时的宣传性网站完全没有现在的复杂,基本上就2-3个页面,动态网站多一点也不会超过10个界面,何况当时有很多免费空间,所以当时他那几百几百赚很轻松、很敏捷、很高效啊,我曾“偷师”他的网站代码,我记不清楚全部,只能yy一下大概的模样,大家可以小看一下,php的:
<?php $var=file("./产品列表.txt");//当时用access还不如用 txt if(!$var || is_array($var) || count($var)==0) exit("系统繁忙,请稍后再试"); $fix=array("中国XXX最大的网站","只有我们的产品是正宗的","假一罚十绝不坑人","上哪能买到如此好的XXX不要再犹豫了"); ?> <title><?php echo $fix[0]?></title>
....这里是乱七八糟的 假大空神句 ....... |
....这里是同类网站各自互相支持穿插的广告,如:“激起你心中的虎”或“用了XXX后腿脚好了,也不起夜了”等等。 |
echo ' //商品标题很耸听,意思是不买 你会后悔白来世上一趟 ?> |
........注意这里已然是页面的尾部了.......... //注意当时 备案 真的不严 //注意当时我朋友不会脚本,因此该页面要刷一次才会变化当前时间。 |
好,以上就是我朋友赖以生存的一个基本技能。据说换个同类型的客户他只要把“产品列表.txt” 换换内容,然后把td的背景图片换掉,页面立马洗新革面,我朋友当时非常 严肃的告诉我,他已经实现“产品化”的开发模式了。我膜拜的五体投地,因为我当时初学asp时,绝对没有这么“可配置化”。
网页里面的广告和语句大家不要太纠结,反正当时作为小白的我看了这个网页,很想花钱试一试,不过我朋友告诉我我还没到用的时候。我追问“什么时候能用”,我朋友“扇”了我一巴掌。
接下来我们切入正题,解释一下上文中的知识点。
一、数组最基本的表现形式
$fix=array("内容1","内容2","内容3");这个是php数组最基本的表现方式。请原谅我不想把广告词再打一遍,太恶心了。
你想堆积多少内容均可,只要你写的下。当你要调用里面的内容时,你只要从“0”开始计数进行调用,如$fix[0],$fix[1]...$fix[n]。
注意:为什么要从0开始。一个是因为"php老大"就是这么设计的,另外一个是因为 这个最最基本数组的真身是
$fix=array(0=>"内容1",1=>"内容2",2=>"内容3“);
"=>"这个符号是被省略了,这个符号 左边是 键,右边是值,一般很多教科书上 会解释成 "$key=>$value".大家不要纠结为什么左边是$key右边是$value,我告诉大家是习惯性写法,你要写成 $ss=>$bb,都代表左边那个是键,右边那个是值。
所以:任何一个形式的数组 都会有键和值。省不省略看你的,不管你省不省,反正我省了。
拓展一下:既然有键值,那么就可以更改键值。
譬如 $fix=array("夸张的网站名称"=>"中国最大的XXX网站 ","扯蛋的产品品牌"=>"只有我们是最正宗的","听了想吐的广告语"=>"假一罚十绝不坑人");
这个时候假如你要把 "扯蛋的产品品牌" 输出到页面,你就不能用 echo $fix[1]; 因为键值 已经被你改了。
应该用 echo $fix['扯蛋的产品品牌'];
二、遍历数组
继续以 $fix=array("内容1","内容2","内容3"); 为例子
1、用foreach是循环小型数组最贴身最适合的方法。
基本语法是:foreach(这里是原始数组 as 这里写每次遍历时设置的变量)
例如:foreach($var as $eachline) echo $eachline; 则会以此输出 内容1.....内容3;
2、很多人知道其实还有一个 while 可以遍历数组
基本语法是:while(list($key,$value)=each($attr))
例如:while(list($key,$value)=each($fix)) echo $key.$value; 则会依次输出 0内容1.。。。。2内容2;
这两种遍历区别这里不讲的太深,后面会讲到,我现在只告诉大家,如果你只是要遍历数据,那么不管啥时候都用foreach。如果你要在遍历过程中还要更改数组的值,那么用while。原因只有一个字,为了"快“。现在生活节奏太快,写程序首要原则是”快“。
至于其他遍历数组的语法还有,我个人认为咱没必要学了,除非你去考秀才,如果是实战 这两种够了,我们也是为了快。
拓展一下,数组里面的值不光可以放字符串,也可以放数组也可以任何形式的变量值.
如 $fix=array("扯蛋的广告词"=>array("前100位订购者,再送200元大礼包","20天精通某语言","孩子不吃饭是因为缺X"));
对于这样的数组,$fix['扯蛋的广告词'] 的值其实是个数组、
譬如 echo $fix['扯蛋的广告词'][1]; 则会输出 "20天精通某语言"
三、数组的赋值
来举个例子:
$fix=array(); 这个数组是空的。
$fix[]="内容1”; 此时就等同于 $fix=array("内容1");或者$fix=array(0=>"内容1");
$fix[]="内容2";此时等同于 $fix=array("内容1","内容2"); 或者 $fix=array(0=>"内容1",1=>"内容2");
$fix['我们在学什么']='php'; 此时等同于 $fix=array(0=>"内容1","我们在学什么"=>"php");
以上赋值都是在数组的尾部赋值,其实还有array_push函数可以赋值,语法是$fix=array_push($fix,"内容1","内容2");效果是一样的,只不过array_push可以一下子加好多值,用'[]'一次一个。
php数组函数很强大,几乎想干什么都能干到,譬如数组的排序、合并、反转、删除等等,大家可以百度一下,由于篇幅问题这里不多讲,函数靠背、靠多用就会,不难。不过呢,到了实战做项目 很多数据的处理都需要通过数据库存储过程、优化的表结构、好的数据排序算法、技巧化的数据读取方式来做,真正实战php里面好多数组的函数基本上是用不到的,譬如你接到了一个1230X这样的项目,要列出所有国人的名字并排序你敢用php数组来遍历着干并且合并、反转吗。当然假如你的客户是面向梵蒂冈或者冰岛的那可以这么干。
不过很多函数譬如, is_array--是否是数组 in_array---是否存在某值,array_key_exists---数组中是否存在某键值等等常用的函数是一定要学会的。如学不会,那你离做领导也不远了。
彩蛋:
上文中有个 $var=file("./产品列表.txt"); 意思是把文本文档一次性读取,并且按照行读取成数组,其中包括换行符。
下一天 讲php伟岸的文件操作,请大家预热。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible
