PHP数组的定义、初始化和数组元素的展示
PHP数组的定义、初始化和数组元素的显示
从ASP初入门到PHP,感觉到PHP的强大之一就是内置函数的丰富,比如先前学习的PHP日期时间函数,读写文件的相关函数等都无不表明了PHP的更专业、更令用户的使用得心应手。
一开始我对PHP函数的丰富功能很兴奋,随着对越来越多近乎变态多的函数接触之后,突然联想到了ASP内置函数的稀少,要完成某项特殊功能,常须自定义函数,随着应用的在增多,自己居然也有了一套常用的函数库。然而现在在PHP中,这些功能早已被标准化、规范化而浓缩为内置函数直接使用,曾经的ASP开发人员成为了PHP的普通用户。
但转念一想,这些函数、这些大量PHP函数的存在,至少说明了PHP的更专业;同时,在我们日常PHP程序处理时应该是很快捷易用的吧,这让开发人员不再为些基础功能、细节功能而去自定义函数,把主要的精力集中在组建更强大的程序模块上。所以,我更加坚定了一看PHP函数到底的信念,不过我想在以后的开发过程中,PHP函数手册应该属于随身书了。
当然,关于ASP和PHP孰优孰劣的争论就无需多讨论,学习并了解能让自己了解真相。
言正传,PHP函数太多,防止遗忘,所以每次看完一类函数后我都做个总结和收集工作,方便起见就写篇日志。
1,数组的定义和初始化
什么是数组?数组是一种编程结构,它是一个存储一组或一系列数值的变量。
比如人口普查时对个人的身份登记,如姓名、性别、民族、出生等就可作为数组。
PHP中创建数组使用array()结构来定义,比如:
$people=array('name','sex','nation','brith'); |
而如何显示数组中的各元素的值,我们则是使用从0开始的索引,索引号在变量名称后的方括号中,比如:
$people=array('name','sex','nation','birth'); echo $people[2]; ?> |
输出的$people[2]就是显示的是nation(索引第一项从0计数)。
PHP除了支持数字索引数组以外,还支持相关数组。所谓相关数组,就是可自定义关键字来替换不直观的数字索引,比如:
$peoples=array('xm'=>'name','xb'=>'sex','mz'=>'nation','cs'=>'birth'); echo $peoples['cs']; ?> |
使用相关数组使得输出的选择很直观(不需要预先计算索引号然后输出),定义的关键字和值之间使用“=>”符号定义。
根据PHP数组元素的两种显示方式,还可以如变量一样无需array()声明和初始化,直接自动创建数字。比如
$people[0]='name'; $people[1]='sex'; $people[2]='nation'; $people[3]='brith'; |
或者
$peoples['xm']='name'; $peoples['xb']='sex'; $peoples['mz']='nation'; $peoples['cs']='birth'; |
该数组的大小根据所增加元素的多少动态的变化。
2,数组元素的显示
在如上使用的无论$people[2]也好,无论$peoples['cs']也好,都只是输出已知的明确位置的数组元素值,如何快速输出所有或部分的数组元素,使用循环语句无疑是最快的方法。
$people=array('name','sex','nation','birth'); for ($i=0;$i????echo "$people[$i] "; ?> |
除了使用了解循环次数的for循环以外,还可以使用对循环次数无须要求的foreach语句。
$people=array('name','sex','nation','birth'); foreach($people as $xiangmu) ????echo $xiangmu; ?> |
$xiangmu变量将保存数组中的各元素值,依次显示出来。当然,为了输出的数据能间隔区分,可在数组元素之后输出空格:
echo $xiangmu." ";
注:英文句号(.)可将字符串连接合并成新的字符串,参见亲密接触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



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
