search
Homephp教程php手册学习使用PHP数组

数组

PHP4.0中共有超过30个新的数组相关函数。其中很多通用函数允许你检查给定数组中是否存在特定对象、对数组元素计数、增加或删除元素,或对元素排序。



如果你有很大的一个数组,而所要完成的仅是找出一个存在的给定值,你可以使用in_array()以返回true 或 false。如下代码将输出“Not found in this array”——因为你将在$namesArray中寻找一个并不存在的“Alber ”。

$namesArray = array("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John");

$lookingFor = "Albert";

if (in_array($lookingFor, $namesArray)) {

echo "You've found it!";

} else {

echo "Not found in this array!";

}

?>

如果你改变了$lookingFor的值,将其变为“Mary”,你将得到消息“You've found it!”——因为“Mary”是$namesArray的一部分。

如果希望对数组元素计数,你可以使用count()函数:

$namesArray = array("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John");

$count = count($namesArray); ?>

$count值将为7。

你可以对任何数组添加元素,无论是在已存在数组的开始或末尾。你也可以使用函数以创建一个包含两个或多个数组元素的新数组。合并时每个数组将按需要的顺序排列。如果你的数组已经有内部的排序,你需要对新的合并数组重排序。
让我们从对已存在数组的末尾增添元素开始,使用函数array_push():



/* 创建原始数组 */

$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");

/* 加入到原始数组中 */

array_push($fruitArray, "grape", "pineapple", "tomato");

/* 通过其键值列出每个元素*/

while (list($key,$value) = each($fruitArray)) {

echo "$key : $value
";

}

?>

这将显示:

0 : apple

1 : orange

2 : banana

3 : kiwi

4 : pear

5 : grape

6 : pineapple

7 : tomato

当你需要对数组开头添加元素时,代码非常类似。不同处只是函数名:array_unshift() 而不是array_push()。



/* 创建原始数组 */

$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");

/* 加入到原始数组中 */



array_unshift($fruitArray, "grape", "pineapple", "tomato");

/* 通过其键值列出每个元素*/

while (list($key,$value) = each($fruitArray)) {

echo "$key : $value
";

}

?>

这将显示:

0 : grape

1 : pineapple

2 : tomato

3 : apple

4 : orange

5 : banana

6 : kiwi

7 : pear

函数array_merge()合并两个或更多的数组。

/* 创建原始数组 */

$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");

/* 创建第二个数组 */

$vegArray = array("carrot", "green beans", "asparagus", "artichoke", "corn");

/* 合并为一个数组 */

$goodfoodArray = array_merge($fruitArray, $vegArray);

/* 通过其键值列出每个元素*/

while (list($key,$value) = each($goodfoodArray)) {

echo "$key : $value
";

}

?>

这将显示:

0 : apple

1 : orange

2 : banana

3 : kiwi

4 : pear

5 : carrot

6 : green beans

7 : asparagus

8 : artichoke

9 : corn

现在已经对数组进行了增加元素和合并,现在来练习删除元素函数。你可以使用函数array_pop()从一数组末尾删除一个元素。如果使用函数array_shift(),则从一数组开头删除一个元素。而实际上当你从数组删除元素时,此元素对你而言仍然可用——当你从已存在的数组中对元素进行pop 或 shift时。

使用array_pop()函数从数组末尾删除一个值:



/* 创建一数组*/

$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");

/* 在末尾弹出某值 */

$popped = array_pop($fruitArray);

/* 列出新数组内容,以及弹出的值*/

while (list($key,$value) = each($fruitArray)) {

echo "$key : $value
";

}

echo "
and finally, in $popped: $popped";

?>
这将显示:



0 : apple

1 : orange

2 : banana

3 : kiwi

and finally, in $popped: pear

Next, delete an element from the end of an array: ???????????

下面,从数组末尾删除某值:



/* 创建一数组*/

$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");

/* 从数组头部移出某值 */

$shifted = array_shift($fruitArray);

/* 列出新数组的内容以及移出的值*/

while (list($key,$value) = each($fruitArray)) {

echo "$key : $value
";

}

echo "
and finally, in $shifted: $shifted";

?>

这将显示:

0 : orange

1 : banana

2 : kiwi

3 : pear

and finally, in $shifted: apple

有很多函数可以帮助你对数组元素排序。但我将会演示基本的排序以帮助你了解其过程:

/* 创建原始数组 */

$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");

/* 排序 */

sort($fruitArray);

/* 对其重设以正确从头到尾显示数组 */

/* 通过其键值列出每个元素*/

while (list($key,$value) = each($fruitArray)) {

echo "$key : $value
";

}

?>

这将显示:

0 : apple

1 : banana

2 : kiwi

3 : orange

4 : pear



Statement
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
五个精选的Go语言开源项目,带你探索技术世界五个精选的Go语言开源项目,带你探索技术世界Jan 30, 2024 am 09:08 AM

在当今科技快速发展的时代,编程语言也如雨后春笋般涌现出来。其中一门备受瞩目的语言就是Go语言,它以其简洁、高效、并发安全等特性受到了许多开发者的喜爱。Go语言以其强大的生态系统而著称,其中有许多优秀的开源项目。本文将介绍五个精选的Go语言开源项目,带领读者一起探索Go语言开源项目的世界。KubernetesKubernetes是一个开源的容器编排引擎,用于自

使用C#中的Array.Sort函数对数组进行排序使用C#中的Array.Sort函数对数组进行排序Nov 18, 2023 am 10:37 AM

标题:C#中使用Array.Sort函数对数组进行排序的示例正文:在C#中,数组是一种常用的数据结构,经常需要对数组进行排序操作。C#提供了Array类,其中有Sort方法可以方便地对数组进行排序。本文将演示如何使用C#中的Array.Sort函数对数组进行排序,并提供具体的代码示例。首先,我们需要了解一下Array.Sort函数的基本用法。Array.So

Go语言开发必备:5个热门框架推荐Go语言开发必备:5个热门框架推荐Mar 24, 2024 pm 01:15 PM

《Go语言开发必备:5个热门框架推荐》Go语言作为一门快速、高效的编程语言,受到越来越多开发者的青睐。为了提高开发效率,优化代码结构,很多开发者选择使用框架来快速搭建应用。在Go语言的世界中,有许多优秀的框架可供选择。本文将介绍5个热门的Go语言框架,并提供具体的代码示例,帮助读者更好地理解和使用这些框架。1.GinGin是一个轻量级的Web框架,拥有快速

使用Golang的Web框架Echo框架实现分布式任务调度使用Golang的Web框架Echo框架实现分布式任务调度Jun 24, 2023 am 11:49 AM

随着互联网的发展和信息技术的进步,大数据时代已经来临,数据分析、机器学习等领域也得到了广泛的应用。在这些领域中,任务调度是一个不可避免的问题。如何实现高效的任务调度,对于提高效率至关重要。在本篇文章中,将介绍如何使用Golang的Web框架Echo框架实现分布式任务调度。一、介绍Echo框架Echo是一个高性能、可伸缩、轻量级的GoWeb框架。它基于HTT

Laravel开发:如何使用Laravel Echo和Pusher实现WebSockets通信?Laravel开发:如何使用Laravel Echo和Pusher实现WebSockets通信?Jun 13, 2023 pm 05:01 PM

Laravel是一个流行的PHP框架,具有高度可扩展性和高效性,它提供了很多强大的工具和库,让开发者可以快速构建高质量的Web应用程序。其中,LaravelEcho和Pusher是两个非常重要的工具,通过它们可以很容易地实现WebSockets通信,本文将详细介绍如何在Laravel应用程序中使用这两个工具。什么是WebSockets?WebSockets

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code<form name="myform"

简单明了的PHP array_merge_recursive()函数使用方法简单明了的PHP array_merge_recursive()函数使用方法Jun 27, 2023 pm 01:48 PM

在进行PHP编程时,我们常常需要对数组进行合并。PHP提供了array_merge()函数来完成数组合并的工作,不过当数组中存在相同的键时,该函数会覆盖原有的值。为了解决这个问题,PHP在语言中还提供了一个array_merge_recursive()函数,该函数可以合并数组并保留相同键的值,使得程序的设计变得更加灵活。array_merge

PHP中echo关键字的作用和使用方法详解PHP中echo关键字的作用和使用方法详解Jun 28, 2023 pm 08:12 PM

PHP中echo关键字的作用和使用方法详解PHP是一种广泛使用的服务器端脚本语言,它在网页开发中被广泛应用。而echo关键字是在PHP中用于输出内容的一种方法。本文将详细介绍echo关键字的作用和使用方法。作用:echo关键字的主要作用是将内容输出到浏览器。在网页开发中,我们需要将数据动态地呈现到前端页面上,这时就可以使用echo关键字将数据输出到页面上。e

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.