學習使用PHP數(shù)組
Jun 21, 2016 am 09:14 AM數(shù)組
PHP4.0中共有超過30個新的數(shù)組相關(guān)函數(shù)。其中很多通用函數(shù)允許你檢查給定數(shù)組中是否存在特定對象、對數(shù)組元素計數(shù)、增加或刪除元素,或?qū)υ嘏判颉?br>
如果你有很大的一個數(shù)組,而所要完成的僅是找出一個存在的給定值,你可以使用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的值,將其變?yōu)椤癕ary”,你將得到消息“You've found it!”——因為“Mary”是$namesArray的一部分。
如果希望對數(shù)組元素計數(shù),你可以使用count()函數(shù):
$namesArray = array("Joe", "Jane", "Bob", "Mary", "Paul", "Eddie", "John");
$count = count($namesArray); ?>
$count值將為7。
你可以對任何數(shù)組添加元素,無論是在已存在數(shù)組的開始或末尾。你也可以使用函數(shù)以創(chuàng)建一個包含兩個或多個數(shù)組元素的新數(shù)組。合并時每個數(shù)組將按需要的順序排列。如果你的數(shù)組已經(jīng)有內(nèi)部的排序,你需要對新的合并數(shù)組重排序。
讓我們從對已存在數(shù)組的末尾增添元素開始,使用函數(shù)array_push():
/* 創(chuàng)建原始數(shù)組 */
$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
/* 加入到原始數(shù)組中 */
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
當你需要對數(shù)組開頭添加元素時,代碼非常類似。不同處只是函數(shù)名:array_unshift() 而不是array_push()。
/* 創(chuàng)建原始數(shù)組 */
$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
/* 加入到原始數(shù)組中 */
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
函數(shù)array_merge()合并兩個或更多的數(shù)組。
/* 創(chuàng)建原始數(shù)組 */
$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
/* 創(chuàng)建第二個數(shù)組 */
$vegArray = array("carrot", "green beans", "asparagus", "artichoke", "corn");
/* 合并為一個數(shù)組 */
$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
現(xiàn)在已經(jīng)對數(shù)組進行了增加元素和合并,現(xiàn)在來練習刪除元素函數(shù)。你可以使用函數(shù)array_pop()從一數(shù)組末尾刪除一個元素。如果使用函數(shù)array_shift(),則從一數(shù)組開頭刪除一個元素。而實際上當你從數(shù)組刪除元素時,此元素對你而言仍然可用——當你從已存在的數(shù)組中對元素進行pop 或 shift時。
使用array_pop()函數(shù)從數(shù)組末尾刪除一個值:
/* 創(chuàng)建一數(shù)組*/
$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
/* 在末尾彈出某值 */
$popped = array_pop($fruitArray);
/* 列出新數(shù)組內(nèi)容,以及彈出的值*/
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: ???????????
下面,從數(shù)組末尾刪除某值:
/* 創(chuàng)建一數(shù)組*/
$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
/* 從數(shù)組頭部移出某值 */
$shifted = array_shift($fruitArray);
/* 列出新數(shù)組的內(nèi)容以及移出的值*/
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
有很多函數(shù)可以幫助你對數(shù)組元素排序。但我將會演示基本的排序以幫助你了解其過程:
/* 創(chuàng)建原始數(shù)組 */
$fruitArray = array("apple", "orange", "banana", "kiwi", "pear");
/* 排序 */
sort($fruitArray);
/* 對其重設(shè)以正確從頭到尾顯示數(shù)組 */
/* 通過其鍵值列出每個元素*/
while (list($key,$value) = each($fruitArray)) {
echo "$key : $value
";
}
?>
這將顯示:
0 : apple
1 : banana
2 : kiwi
3 : orange
4 : pear

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

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.

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

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

With the rapid development of Internet technology, Web applications have become an indispensable part of people's lives and work. How to build and deploy web applications more efficiently has also become a hot topic. This article will introduce how to use Golang's web framework Echo framework and Docker to build an efficient web application. 1. About the Echo Framework The Echo Framework is a high-performance web framework written in Golang. It is characterized by being lightweight, simple, easy to use and efficient. ByEch
