PHP开发札记系列(九)- 数组(一)
PHP开发笔记系列(九)- 数组(一)
??? 最近在做项目的时候,经常需要用到关联数组的处理,发现PHP里面有很多自带的数组处理函数,使用起来非常方便,而且效率不错,重新整理一下, 作为《PHP开发笔记系列(XAMPP+PhpEclipse+XDebug)》 的第九篇,《PHP开发笔记系列(九)- 数组(一)》,记录PHP数组的相关操作。
??? 1. Php的数组定义
??? Php的数组类似于JAVA中Map的概念,数组中的元素有一个特殊的标识符来区分,称为键(Key),而每个键对应的就是值(Value)。因此一个键(Key)和值(Value)的组合组成数组中的一个元素。Php数组中的元素比较灵活,每个元素不必是同一种类型,例如可以是整型、字符串等。
??? 2. Php的数组赋值
???? Php的键可以是数值键(numerical),也可以是关联键(associative)。数值键与值没有真正关系,只是值在数组中的位置,而关联键则指向值。
?
$data[0] = '1st value'; $data[1] = '2nd value'; $data[2] = '3rd value';$data['1st'] = '1st value'; $data['2nd'] = '2nd value'; $data['3rd'] = '3rd value';echo $data[0];echo $data['3rd'];
?
??? 3. Php的多维数组
???? Php的数组可以签到,形成多维数组。可以通过以下方式定义:
$data['school1']['grade1'] = 'grade 1'; $data['school1']['grade2'] = 'grade 2';$data['school1']['grade3'] = 'grade 3';echo $data['school1']['grate1'];
?
??? 4. 创建数组
??? 除了使用上面的方式创建数组外,还可以通过构造函数array()进行数组创建,如下:
// 创建空数组$data = array();// 创建数值键非空数组$numeric_data = array('1st value', '2nd value', '3rd value');// 创建关联键非空数组$map_data = array('1st'=>'1st value', '2nd'=>'2nd value', '3rd'=>'3rd value');// 创建数组$multi_map_data = array( 's1'=>array('g1'=>'grade1', 'g2'=>'grade2'), 's2'=>array('g3'=>'grade3', 'g4'=>'grade4') );
?
??? 5. 使用list()提取数组
??? list()函数与array()类似,但它可以在一次操作中从一个数组内提取多个值,同时为多个变量赋值。从数据库或文件中提取信息时,这种构造特别有用。
??? 例如,一个文件school.txt,需要从该文件读取信息,文件的每一行都包含学习的名称、年级、班级、学生姓名等信息,每一项使用“,”进行分割,如
file:array-list.txtSchool1,grade1,class1,jackSchool1,grade1,class2,dannySchool1,grade2,class1,mikeSchool1,grade2,class2,lilySchool2,grade1,class1,dickSchool2,grade1,class2,marySchool2,grade2,class1,johnySchool2,grade2,class2,smart... ...
?
??? 可以通过一个简单的循环使用list()来读取每一行,将各部分数据赋给变量,按照需要格式化并输出数据,代码如下:
?
file: array-list.phpurl: http://localhost:88/array/array-list.php<?php $fp = fopen('school.txt', 'r'); while ($line = fgets($fp, 1024)) { list($school, $grade, $class, $student) = explode(',', $line); echo 'School: '.$school.' | '; echo 'Grade: '.$grade.' | '; echo 'Class: '.$class.' | '; echo 'Student: '.$student.'<br/>'; } fclose($fp);?>
??? 6. 测试变量是否为数组
??? 使用数组时,有时候需要使用某个特定变量是否为一个数组。内置函数is_array()可以判断变量是否为数组,如果是返回TRUE,否则返回FALSE。
?
file: is_array.phpurl: http://localhost:88/array/is_array.php<?php $arr = 1; $arr1 = array(); $arr2 = array('id'=>1); echo is_array($arr) ? 'TRUE' : 'FALSE'; echo '<br/>'; echo is_array($arr1) ? 'TRUE' : 'FALSE'; echo '<br/>'; echo is_array($arr2) ? 'TRUE' : 'FALSE'; echo '<br/>'; ?>
?
??? 7. 输出数组
??? 输出数组的方式很多,可以使用foreach、for、while等进行数组元素遍历,代码如下:
file: output-array.phpurl: http://localhost:88/array/output-array.php<?php $data[0] = '1st value'; $data[1] = '2nd value'; $data[2] = '3rd value'; foreach ($data as $d) { echo $d.'<br/>'; } echo '======================'.'<br/>'; $i = 0; while ($i < count($data)) { echo $data[$i].'<br/>'; $i++; } echo '======================'.'<br/>'; for ($i = 0; $i < count($data); $i++) { echo $data[$i].'<br/>'; } echo '======================'.'<br/>'; $map['1st'] = '1st value'; $map['2nd'] = '2nd value'; $map['3rd'] = '3rd value'; foreach ($map as $key => $value) { echo $key.':'.$value.'<br/>'; } ?>
?
??? 今天先写到这,改天继续。
?? 本文地址:http://ryan-d.iteye.com/blog/1566123

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

In today's era of rapid technological development, programming languages are springing up like mushrooms after a rain. One of the languages that has attracted much attention is the Go language, which is loved by many developers for its simplicity, efficiency, concurrency safety and other features. The Go language is known for its strong ecosystem with many excellent open source projects. This article will introduce five selected Go language open source projects and lead readers to explore the world of Go language open source projects. KubernetesKubernetes is an open source container orchestration engine for automated

Laravel is a popular PHP framework that is highly scalable and efficient. It provides many powerful tools and libraries that allow developers to quickly build high-quality web applications. Among them, LaravelEcho and Pusher are two very important tools through which WebSockets communication can be easily implemented. This article will detail how to use these two tools in Laravel applications. What are WebSockets? WebSockets

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values of the same keys, making the program design more flexible. array_merge

The most popular Go frameworks at present are: Gin: lightweight, high-performance web framework, simple and easy to use. Echo: A fast, highly customizable web framework that provides high-performance routing and middleware. GorillaMux: A fast and flexible multiplexer that provides advanced routing configuration options. Fiber: A performance-optimized, high-performance web framework that handles high concurrent requests. Martini: A modular web framework with object-oriented design that provides a rich feature set.

Detailed explanation of the role and usage of the echo keyword in PHP PHP is a widely used server-side scripting language, which is widely used in web development. The echo keyword is a method used to output content in PHP. This article will introduce in detail the function and use of the echo keyword. Function: The main function of the echo keyword is to output content to the browser. In web development, we need to dynamically present data to the front-end page. At this time, we can use the echo keyword to output the data to the page. e

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

"Go Language Development Essentials: 5 Popular Framework Recommendations" As a fast and efficient programming language, Go language is favored by more and more developers. In order to improve development efficiency and optimize code structure, many developers choose to use frameworks to quickly build applications. In the world of Go language, there are many excellent frameworks to choose from. This article will introduce 5 popular Go language frameworks and provide specific code examples to help readers better understand and use these frameworks. 1.GinGin is a lightweight web framework with fast
