Home php教程 php手册 php学习 数组课件第1/2页

php学习 数组课件第1/2页

Jun 13, 2016 pm 12:28 PM
php subscript code name name exist string study array integer index identify

下标:数组中的识别名称 也就是字符串或整数在数组中的代号

数组中有几个索引值就被称为几维数组。
索引值:索引是对数据库表中一列或多列的值进行排序的一种结构。

    数组分类
在PHP数组被分为两种:
索引数组:索引(indexed)索引值是整数,以0开始,当通过位置来标识东西时用索引数组。
关联数组:关联(associative)关联以字符串做索引值,索引值为列名,用语访问列的数据。

    数组通常用赋值的方式
一般情况下数组赋值有两种方式:
$a[1]="dsadsadsa";
$b[2]="dsadsadsad";
使用array函数:
$a=array("dsads","dsadsa",321312);
一维数组:数组的索引值(下标)只有一个的时候称之为一维数组。
数组直接赋值的格式:
$数组变量名[索引值]=资料内容;
注意:索引值可以是字符串也可以是整数  但是1与“1”是不同的 它们一个属于整数一个属于字符串。

同名没有给予索引值的数组是按照顺序排列的。
实例:
      $a=array(1,2,3,4,5,6);
    $b=array("one", "two", "three");
    $c=array(0=>"aaa",1=>"bbb",2=>"ccc");
    $d=array("aaa",6=>"bbb","ccc");
    $e=array("name"=>"zhang", "age"=>20);
  ?>
    二维数组
多维数组的格式:
$a[0][]="dsadas";
$a[0][]="dsadsa";  这组是$a下的0索引值下的1和2
如果用array函数声明格式如下:
$a=array("dsadsa","dsadas",21,array("dsadsa","dsadas"));

    数组的遍历
foreach循环结构:
foreach 仅用与数组的循环 两种格式
foreach(array_exprssion(数组表达) as $value);
foreach(array_exprssion(数组表达) as $key=>$value);
第一种格式遍历给定了array_exprssion数组。每一次循环中当前的值都被赋给我$calue,并且数组内部的指针向前移动一步。
第二种格式做同样的事,只除了当前单元的键值也会在每次循环中被赋给变量 $key。 
当 foreach 开始执行时,数组内部的指针会自动指向第一个单元。此外注意foreach 所操作的是指定数组的一个拷贝,而不是该数组本身
  $arr=array(10,20,30,40,50,60);
  foreach($arr as $k=>$v){
    echo "$k=>$v
";
  }

输出结果:0=>10 1=>20 2=>30 3=>40 4=>50 5=>60//下标=>整数
    联合使用list(),each()和while循环
each():
  $arr=array(1,2,3,4,5);
  $a=each($arr);  
  print_r($a);
输出结果:Array ( [1] => 1 [value] => 1 [0] => 0 [key] => 0 ) 
拿取数组第一位的值value 下标key
list():
  $arr3=array("a","b","c");    
  list($key,$value)=each($arr3);

  echo $key."
".$value;
输出结果:0 a     list()可以说是用一步操作给一组变量赋值 仅能用于数字索引的数组并假定数字索引从 0 开始。
while循环
  $arr=array(1,2,3,4,5,6,7,8,9,);
  while(list($key,$value)=each($arr)){
      $key++;
      echo $key."=>".$value;
      echo "
";
  }
  echo "
";
输出结果:1=>1 2=>2 3=>3 4=>4 5=>5 6=>6 7=>7 8=>8 9=>9
reset()数组指针重定向
在执行 each() 之后,数组指针将停留在数组中的下一个单元或者当碰到数组结尾时停留在最后一个单元。
is_array检测变量是否为数组真返回ture假false
  $arr=array(1,2,3,4,5,6,"saas");
  while(list($k,$v) = each($arr))
  {
    if(is_array($arr))
    {
      $x += $v;
      echo $x;
    }
    else
    {
      $x += $k;
    }
  }
这个范例不能完全体现is_array的功能,但是可以参考。
    数组的指针
next():负责将指针向后移动
prve():负责将指针向前移动
end():会将指针指向数组最后一个元素
reset():将目前指针无条件移至第一个索引位置
语法格式:mixed next(数组名称)
  $arr=(array(1,2,3,4,5));
  echo end($arr);
输出结果:5
    key()与current()和count()
key()的函数是读取目前指针所指向的资料的索引值。
current()的函数则是读取目前指针所指向资料的内容资料。
count()的函数是用来计算数组中所有元素的个数,也就是说函数会传回目标数组的长度值。
格式:int count(数组名称);

key():从关联数组中取得键名
$array = array('fruit1' => 'apple','fruit2' => 'orange','fruit3' => 'grape','fruit4' => 'apple','fruit5' => 'apple');
  while ($fruit_name = current($array)) {
      if ($fruit_name == 'apple') {
        echo key($array).'
';
      }
      next($array);
  }
输出结果:fruit1,fruit4,fruit5

current():返回数组中的当前单元
  $transport = array('foot', 'bike', 'car', 'plane');
  $mode = current($transport); // $mode = 'foot';
  $mode = next($transport);    // $mode = 'bike';
  $mode = current($transport); // $mode = 'bike';
  $mode = prev($transport);    // $mode = 'foot';
  $mode = end($transport);     // $mode = 'plane';
  $mode = current($transport); // $mode = 'plane';
注意看范例 返回数组中的当前单元

count():计算数组中单元的个数
  $arr=array(1,2,3,4,5,6);
  echo count($arr);
输出结果:6

    array_change_key_case()
array_change_key_case返回字符串键名全为小写或者大写的数组
其中包含的形态函数有两个[CASE_UPPER]转换为大写,[CAS_LOWER]转换为小写。
  $input_array = array("FirSt" => 1, "SecOnd" => 4);
  print_r(array_change_key_case($input_array, CASE_UPPER));
输出结果:Array ( [FIRST] => 1 [SECOND] => 4 ) 

    array_chunk()
array_chunk()次函数会将目标数组的资料内容,以指定索引个数,分解成数个小型数组包含在原数组中。
  $arr=array(1,2,3,4,5,6);
  $a=array_chunk($arr,3);
  print_r($a);
输出结果:Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [1] => Array ( [0] => 4 [1] => 5 [2] => 6 ) )
也就是等于用数组单元个数的总和除以3

    array_count_values
array_count_values  用来计算目标数组中各值出现的次数
语法格式:array_count_values(目标数组)
此函数所传回结果值,会以原数组的内容资料作为索引,以数组的形态表现。
  $arr=array(1,2,3,3,2,6);
  print_r(array_count_values($arr));
输出结果:Array ( [1] => 1 [2] => 2 [3] => 2 [6] => 1 )  
    

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
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
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)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

See all articles