Home  >  Article  >  Backend Development  >  Summary of using php array_PHP tutorial

Summary of using php array_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:19:07929browse

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

1.2 Array() constructs an array

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

2. Multidimensional array

Take associative array as an example

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

2. foreach statement

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

3. while loop

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

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

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

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

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

   1: <?php
   2: $address = "www.ido321.com";
   3: //输出:存在
   4: if(in_array($address,$dwqs2)){
   5:     echo "存在";
   6: }
   7: else{
   8:     echo "不存在";
   9: }
  10: ?>
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
   2:     echo count($dwqs2);
   3: ?>

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

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

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
   2: //输出:Array(php => 2,mysql => 1,linux => 1)
   3: print_r(array_count_values($a));
   4: ?>
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...
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