Home > Backend Development > PHP Tutorial > Summary of using php array_PHP tutorial

Summary of using php array_PHP tutorial

WBOY
Release: 2016-07-13 10:19:07
Original
966 people have browsed it

Summary of using php arrays

In PHP, arrays are divided into two categories: indexed arrays and associative arrays. The two can be used alone or mixed.

1. One-dimensional array

The definition of one-dimensional array is also very simple. There are two commonly used methods:

1.1 Direct assignment

   1: <?php
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   2: $dwqs[0] = "1“;
Copy after login
   3: $dwqs[1] = "我的博客:";
Copy after login
   4: $dwqs[2] = "www.ido321.com";
Copy after login
   5: $dwqs[3] = "程序爱好者:";
Copy after login
   6: $dwqs[4] = "QQ群:259280570";
Copy after login
   7: $dwqs[5] = "欢迎你的加入";
Copy after login
   8: ?>
Copy after login
Copy after login

1.2 Array() constructs an array

   1: <?php
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   2: $dwqs = array(1,"我的博客","www.ido321.com","程序爱好者:","QQ群:259280570","欢迎你的加入");
Copy after login
   3: ?>
Copy after login
Copy after login

2. Multidimensional array

Take associative array as an example

   1: <?php
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   2: $dwqs1= array(
Copy after login
   3:     "编号" => array(1,2,3),
Copy after login
   4:     "博客" => array("独立博客","CSDN","博客园"),
Copy after login
   5:     "地址" => array("www.ido321.com","blog.csdn.net/u011043843","www.cnblogs.com/ido321")  
Copy after login
   6: ?>
Copy after login
Copy after login
2. Array traversal In PHP, there are three commonly used array traversal methods: 1. for loop
   1: <?php
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   2: for($i = 0; $i < count($dwqs); $i&#43;&#43;){
Copy after login
   3:     echo "$dwqs[i]<br/>";
Copy after login
   4: ?>
Copy after login
Copy after login
Copy after login

2. foreach statement

   1: //第一种方式
Copy after login
   2: <?php
Copy after login
   3: foreach($dwqs as $value){
Copy after login
   4:     echo "$value<br/>";
Copy after login
   5: ?>
Copy after login
Copy after login
   6:  
Copy after login
   7: //第二种方式
Copy after login
   8: <?php
Copy after login
   9: foreach($dwqs1 as $key=>$value){
Copy after login
  10:     echo $key."=>".$value;
Copy after login
  11: ?>
Copy after login

3. while loop

   1: <?php
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   2: while(list($key,$value) = each($dwqs1)){
Copy after login
   3:     echo $key.":".$value;
Copy after login
   4: ?>
Copy after login
Copy after login
Copy after login

3. Some array-related functions (use the print_r() function to output the array content)

PHP’s array function is very powerful and is one of the most commonly used data types. Its processing function also has powerful and efficient characteristics.

1. Array key/value operation function

1.1 Function array_values(): Returns the values ​​of all elements in the array. Just pass in the array name, do not retain the key name, and the returned array will be re-indexed starting from 0.

   1: <?php
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   2: $dwqs2 = array("ID" => 1,"博客" => "www.ido321.com","程序爱好者" => "QQ群:259280570");
Copy after login
   3: //输出:Array([0]=>1,[1]=>www.ido321.com,[2]=>QQ群:259280570)
Copy after login
   4: print_r(array_values($dwqs2));
Copy after login
   5: //输出:array("ID" => 1,"博客" => "www.ido321.com","程序爱好者" => "QQ群:259280570");
Copy after login
   6: print_r($dwqs2);
Copy after login
   7:  
Copy after login
   8: ?>
Copy after login
Copy after login

1.2 Function array_keys(): Returns the key name in the array.

   1: <?php
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   2: //输出所有键名:Array([0]=>ID,[1]=>博客,[2]=>程序爱好者);
Copy after login
   3: print_r(array_keys($dwqs2)) 
Copy after login
   4: //输出指定键名:Array([0]=>ID)
Copy after login
   5: print_r(array_kays($dqws,'ID'));
Copy after login
   6: ?>
Copy after login
Copy after login

1.3 Function In_array(): Detect whether a certain value exists in the array

   1: <?php
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   2: $address = "www.ido321.com";
Copy after login
   3: //输出:存在
Copy after login
   4: if(in_array($address,$dwqs2)){
Copy after login
   5:     echo "存在";
Copy after login
   6: }
Copy after login
   7: else{
Copy after login
   8:     echo "不存在";
Copy after login
   9: }
Copy after login
  10: ?>
Copy after login
2. Number of arrays and uniqueness 2.1 Function count(): Count the number of elements in an array or the number of attributes in an object
   1: <?php
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   2:     echo count($dwqs2);
Copy after login
   3: ?>
Copy after login
Copy after login

2.2 Function array_unique(): Delete duplicate values ​​in the array, and the returned array key name remains unchanged

   1: <?php
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   2: $a = array('a' => 'php','b' => 'mysql','c' => 'linux','d' => 'php');
Copy after login
   3: //输出:array('a' => 'php','b' => 'mysql','c' => 'linux);
Copy after login
   4: print_r(array_unique($a));
Copy after login
   5: ?>
Copy after login
Copy after login

2.3 Function array_count_values(): Counts the number of occurrences of all values ​​in the array. The returned array uses the value in the original array as the key name, and the key value is the number of times the element appears in the original array

   1: <?php
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
   2: //输出:Array(php => 2,mysql => 1,linux => 1)
Copy after login
   3: print_r(array_count_values($a));
Copy after login
   4: ?>
Copy after login
Copy after login
Copy after login
3. Array sorting
Summary of using php array_PHP tutorialhtml

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/879178.htmlTechArticleSummary of using php arrays In PHP, arrays are divided into two categories: index arrays and associative arrays. The two can be used alone or mixed. 1. One-dimensional array The definition of one-dimensional array is also very simple...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template