Home php教程 php手册 PHP学习之数组的定义和填充

PHP学习之数组的定义和填充

Jun 06, 2016 pm 08:36 PM
filling definition array

先了解一下数组,数组就是把一组数据按顺序放在一起。PHP的数组和其它的语言数组有一点点不同:第一,保存的数据是可以是任何类型的;第二,数组的索引可以是数字,也可以是字符串。

PHP的数组,说白了,就是关联数据每一条数组都是以[索引,值]的形式保存的。其中索引默认是以0开始的数字。在未指定索引时,PHP会从0开始自动生成索引。当指定一个索引,PHP会从你指定索引最大正整数的下一个整数开始。如果你指定的是小数,PHP会取整数部分做为索引。

  另外说说数组其它一些小东西:
array()可以声明一个空数组;
array[] = $value 在数组存在时,追加一个数据;在数组不存时,生成一个数组,并追加数据。
array[$index] = $value 在数组存在时,追加或修改一个数据;在数组不存时,生成一个数组,并追加数据。

  看下面的代码:
代码如下:
// 声明数组
$test01 = array();
// 追加数据
$test01[] = "a"; // array(0 => "a");
// 追加一个索引为"a",数据为"b"的数据
$test01["a"] = "b"; // array(0 => "a", "a" => "b");
// 修改索引为0的数据
$test01[0] = "c"; // array(0 => "c", "a" => "b");
// 另一种声明方法
$test02 = array("a", "b", "c"); // array(0 => "a", 1 => "b", 2 => "c");
// 虽然声明了一个字符串索引的数据,但默认索引还是从0开始
$test03 = array("a" => "a", "b", "c"); // array("a" => "a", 0 => "b", 1 => "c");
// 声明中最大的索引为2,虽然最近是索引是0,但默认索引还是从3开始
$test04 = array(2 => "a", 0=>"b", "c"); // array(2 => "a", 0 => "b", 3 => "c");
// 声明一个小数索引会取其整数部分;指定索引时,会修改之前声明的值
$test05 = array("a", 2.7=>"b", 0=>"c"); // array(0 => "c", 2 => "b");
// 虽然声明了负数索引,但默认索引还是从0开始
$test06 = array(-2 =>"a", "b", "c"); // array(-2 => "a", 1 => "b", 2 => "c");
// 多维数组的定义
$test07 = array($test01, $test02, $test03);

  然后介绍数组的一些填充函数,这些大多可以从手册上查到,所以只作简单的介绍。
range($n, $m); 指定值的范围。如range(2,4)生成数组 array(2,3,4)。
count($array); 取得数组的大小。
array_pad($array, $length, $value); 返回一个长度$length的数组,原不足数组补值为$value,长度足够返回原数组。
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

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

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)

How to remove duplicate elements from PHP array using foreach loop? How to remove duplicate elements from PHP array using foreach loop? Apr 27, 2024 am 11:33 AM

The method of using a foreach loop to remove duplicate elements from a PHP array is as follows: traverse the array, and if the element already exists and the current position is not the first occurrence, delete it. For example, if there are duplicate records in the database query results, you can use this method to remove them and obtain results without duplicate records.

PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values ​​takes a relatively long time.

The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy May 01, 2024 pm 12:30 PM

Methods for deep copying arrays in PHP include: JSON encoding and decoding using json_decode and json_encode. Use array_map and clone to make deep copies of keys and values. Use serialize and unserialize for serialization and deserialization.

PHP array multi-dimensional sorting practice: from simple to complex scenarios PHP array multi-dimensional sorting practice: from simple to complex scenarios Apr 29, 2024 pm 09:12 PM

Multidimensional array sorting can be divided into single column sorting and nested sorting. Single column sorting can use the array_multisort() function to sort by columns; nested sorting requires a recursive function to traverse the array and sort it. Practical cases include sorting by product name and compound sorting by sales volume and price.

Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Apr 30, 2024 pm 03:42 PM

The best practice for performing an array deep copy in PHP is to use json_decode(json_encode($arr)) to convert the array to a JSON string and then convert it back to an array. Use unserialize(serialize($arr)) to serialize the array to a string and then deserialize it to a new array. Use the RecursiveIteratorIterator to recursively traverse multidimensional arrays.

Application of PHP array grouping function in data sorting Application of PHP array grouping function in data sorting May 04, 2024 pm 01:03 PM

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.

The definition and use of full-width characters The definition and use of full-width characters Mar 25, 2024 pm 03:33 PM

What are full-width characters? In computer encoding systems, double-width characters are a character encoding method that takes up two standard character positions. Correspondingly, the character encoding method that occupies a standard character position is called a half-width character. Full-width characters are usually used for input, display and printing of Chinese, Japanese, Korean and other Asian characters. In Chinese input methods and text editing, the usage scenarios of full-width characters and half-width characters are different. Use of full-width characters Chinese input method: In the Chinese input method, full-width characters are usually used to input Chinese characters, such as Chinese characters, symbols, etc.

PHP array merging and deduplication algorithm: parallel solution PHP array merging and deduplication algorithm: parallel solution Apr 18, 2024 pm 02:30 PM

The PHP array merging and deduplication algorithm provides a parallel solution, dividing the original array into small blocks for parallel processing, and the main process merges the results of the blocks to deduplicate. Algorithmic steps: Split the original array into equally allocated small blocks. Process each block for deduplication in parallel. Merge block results and deduplicate again.

See all articles