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

学习使用PHP数组

Jun 21, 2016 am 09:14 AM
array echo quot

数组

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 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

Video Face Swap

Video Face Swap

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

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)

Five selected Go language open source projects to take you to explore the technology world Five selected Go language open source projects to take you to explore the technology world Jan 30, 2024 am 09:08 AM

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

Sort array using Array.Sort function in C# Sort array using Array.Sort function in C# Nov 18, 2023 am 10:37 AM

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

Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Jun 13, 2023 pm 05:01 PM

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

Go language development essentials: 5 popular framework recommendations Go language development essentials: 5 popular framework recommendations Mar 24, 2024 pm 01:15 PM

"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

Detailed explanation of the role and usage of the echo keyword in PHP Detailed explanation of the role and usage of the echo keyword in PHP Jun 28, 2023 pm 08:12 PM

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

What are the most popular golang frameworks on the market? What are the most popular golang frameworks on the market? Jun 01, 2024 pm 08:05 PM

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.

How to use the array_combine function in PHP to combine two arrays into an associative array How to use the array_combine function in PHP to combine two arrays into an associative array Jun 26, 2023 pm 01:41 PM

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

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

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

See all articles