search
Homephp教程php手册PHP数组的定义和填充

PHP数组的定义和填充

Jun 06, 2016 pm 07:56 PM
linuxphpfillingarrayCommunityforumEnter

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 先了解一下数组,数组就是把一组数据按顺序放在一起。PHP的数组和其它的语言数组有一点点不同:第一,保存的数据是可以是任何类型的;第二,数组的索引可以是数字,也可以是字符串。 PHP的数组,说白

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

  先了解一下数组,数组就是把一组数据按顺序放在一起。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,长度足够返回原数组。

PHP数组的定义和填充

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

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.