search
HomeBackend DevelopmentPHP TutorialSummary of using php array_PHP tutorial

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
c语言数组如何初始化c语言数组如何初始化Jan 04, 2023 pm 03:36 PM

C语言数组初始化的三种方式:1、在定义时直接赋值,语法“数据类型 arrayName[index] = {值};”;2、利用for循环初始化,语法“for (int i=0;i<3;i++) {arr[i] = i;}”;3、使用memset()函数初始化,语法“memset(arr, 0, sizeof(int) * 3)”。

php 怎么求2个数组相同的元素php 怎么求2个数组相同的元素Dec 23, 2022 am 10:04 AM

php求2个数组相同元素的方法:1、创建一个php示例文件;2、定义两个有相同元素的数组;3、使用“array_intersect($array1,$array2)”或“array_intersect_assoc()”方法获取两个数组相同元素即可。

用Python实现动态数组:从入门到精通用Python实现动态数组:从入门到精通Apr 21, 2023 pm 12:04 PM

Part1聊聊Python序列类型的本质在本博客中,我们来聊聊探讨Python的各种“序列”类,内置的三大常用数据结构——列表类(list)、元组类(tuple)和字符串类(str)的本质。不知道你发现没有,这些类都有一个很明显的共性,都可以用来保存多个数据元素,最主要的功能是:每个类都支持下标(索引)访问该序列的元素,比如使用语法Seq[i]​。其实上面每个类都是使用数组这种简单的数据结构表示。但是熟悉Python的读者可能知道这3种数据结构又有一些不同:比如元组和字符串是不能修改的,列表可以

c++数组怎么初始化c++数组怎么初始化Oct 15, 2021 pm 02:09 PM

c++初始化数组的方法:1、先定义数组再给数组赋值,语法“数据类型 数组名[length];数组名[下标]=值;”;2、定义数组时初始化数组,语法“数据类型 数组名[length]=[值列表]”。

javascript怎么给数组中增加元素javascript怎么给数组中增加元素Nov 04, 2021 pm 12:07 PM

增加元素的方法:1、使用unshift()函数在数组开头插入元素;2、使用push()函数在数组末尾插入元素;3、使用concat()函数在数组末尾插入元素;4、使用splice()函数根据数组下标,在任意位置添加元素。

php怎么判断数组里面是否存在某元素php怎么判断数组里面是否存在某元素Dec 26, 2022 am 09:33 AM

php判断数组里面是否存在某元素的方法:1、通过“in_array”函数在数组中搜索给定的值;2、使用“array_key_exists()”函数判断某个数组中是否存在指定的key;3、使用“array_search()”在数组中查找一个键值。

php 怎么去除第一个数组元素php 怎么去除第一个数组元素Dec 23, 2022 am 10:38 AM

php去除第一个数组元素的方法:1、新建一个php文件,并创建一个数组;2、使用“array_shift”方法删除数组首个元素;3、通过“print_”r输出数组即可。

详解Go语言中删除数组元素的方法详解Go语言中删除数组元素的方法Mar 22, 2023 pm 03:21 PM

在Go语言中,数组是一种重要的数据类型。它与其他语言的数组一样,是一组相同类型的数据组成,可以通过一个索引来访问数组中的元素。在某些情况下,我们需要从一个数组中删除元素,本文将会介绍在Go语言中如何实现数组删除。

See all articles

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools