Home php教程 php手册 php的array数组和使用实例简明教程

php的array数组和使用实例简明教程

Jun 13, 2016 am 09:40 AM
array php use and Base Example array friend of Concise tutorial language

 最近在教一个朋友php,他没有其他语言的基础。对array的理解和用法有些模糊。所以写了个教程,需要的朋友可以参考下

注意注释部分的文字介绍哦~    代码如下:值[value]) //键名一般用与索引 //类型可以为 int 或 字符串[int是什么可以查php手册] //所以可以这样写 //$array = array(0=>'a',1=>'b'); //你也可以这样写 //array会自动补充索引键名,默认是 int值 由0开始 $array = array('a','b'); //测试 不能用echo 只能用 print_r 打印数组  不要问为什么,做就对了 print_r($array); //输出结果是  Array ( [0] => a [1] => b ) //可以看出,如果你不设定键名[key] 它会自动补充 key //你也可以随意改变 key $array = array(3=>'a',5=>'b'); print_r($array); //结果 Array ( [3] => a [5] => b ) //如果你要读取数组的内容,你可以这么做 echo $array[3]; //结果是 a  //这里用echo 是因为只要不是数组,都可以直接用echo输出 //key可以为 字符串 $array = array('aa'=>'a','bb'=>'b'); print_r($array); //结果是Array ( [aa] => a [bb] => b ) //所以你也可以 echo $array['aa']; 注意字符串都是要用引号括住 //值[value]可以是一个变量,可以是数组 $array = array(0=>array('a','b'),1=>array('c','d')); print_r($array); //结果是 Array ( [0] => Array ( [0] => a [1] => b ) [1] => Array ( [0] => c [1] => d ) ) //这叫二维数组 //读取里面的内容可以是这样 echo $array[0][1]; //结果是b 也可以用  //当然也可以是包含更多的数组 $array = array(0=>array(array('a','b'),array('c','d')),1=>array(array('e','f'),array('g','h'))); //看起来有点乱,要自己慢慢领悟 //回归到实际应用中 实例化一个数租 $array = array(); //模拟一个sql循环 sql多数是用 while循环,我这里做一个简单的for 10次循环  echo '
'; echo '
'; for($i=0;$i'我的名'.$i,'age'=>'我的年纪1'.$i);    // $array[]  加中括号 是要让他生成10 个数组 分别0 - 10   //如果是 $array = array('name'=>'我的名'.i,'age'=>'我的年纪1'.i);    //那么结果只有一条数组。最后一条会代替了前一条 } print_r($array); //结果 Array ( [0] => Array ( [name] => 我的名0 [age] => 我的年纪10 ) [1] => Array ( [name] => 我的名1 [age] => 我的年纪11 ) [2] => Array ( [name] => 我的名2 [age] => 我的年纪12 ) [3] => Array ( [name] => 我的名3 [age] => 我的年纪13 ) [4] => Array ( [name] => 我的名4 [age] => 我的年纪14 ) [5] => Array ( [name] => 我的名5 [age] => 我的年纪15 ) [6] => Array ( [name] => 我的名6 [age] => 我的年纪16 ) [7] => Array ( [name] => 我的名7 [age] => 我的年纪17 ) [8] => Array ( [name] => 我的名8 [age] => 我的年纪18 ) [9] => Array ( [name] => 我的名9 [age] => 我的年纪19 ) [10] => Array ( [name] => 我的名10 [age] => 我的年纪110 ) ) //怎么用呢? ?>  
      '.$value['name'].'  |  '.$value['age'].''; }   ?>
   
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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 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)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1279
29
C# Tutorial
1257
24
What happens if session_start() is called multiple times? What happens if session_start() is called multiple times? Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

H5: Key Improvements in HTML5 H5: Key Improvements in HTML5 Apr 28, 2025 am 12:26 AM

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.

How to use MySQL functions for data processing and calculation How to use MySQL functions for data processing and calculation Apr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

Composer: The Package Manager for PHP Developers Composer: The Package Manager for PHP Developers May 02, 2025 am 12:23 AM

Composer is a dependency management tool for PHP, and manages project dependencies through composer.json file. 1) parse composer.json to obtain dependency information; 2) parse dependencies to form a dependency tree; 3) download and install dependencies from Packagist to the vendor directory; 4) generate composer.lock file to lock the dependency version to ensure team consistency and project maintainability.

Are all list operations supported by arrays, and vice versa? Why or why not? Are all list operations supported by arrays, and vice versa? Why or why not? Apr 26, 2025 am 12:05 AM

No,notalllistoperationsaresupportedbyarrays,andviceversa.1)Arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,whichimpactsperformance.2)Listsdonotguaranteeconstanttimecomplexityfordirectaccesslikearraysdo.

See all articles