


php
It is often necessary to use the traversal of two-dimensional array
. Many people understand the traversal of one-dimensional array
, but for the traversal of two-dimensional array The understanding of traversal means there is nothing you can do. This article will take you to take a look at it.
First of all, if you want to understand the traversal of a two-dimensional array, you must first understand the principle of traversal of a one-dimensional array. Without further ado, let’s go directly to the code.
Two ways to traverse a one-dimensional array:
1. Need to operate on the keys and values of the array
<?php $arr=array('a'=>'php','c'=>'.cn'); foreach($arr as $key=>$value){ echo $key.'=>'.$value."<br>"; }
输出:a=>php c=>.cn
2. Need to operate on the keys and values of the array Operation
<?php $arr2=array('d'=>'ok','b'=>'oya'); foreach($arr2 as $value){ echo $value."<br>"; }
输出: ok oya
The traversal of a one-dimensional array is actually the internal pointer of foreach constantly pointing to the next key-value pair. If the pointer is empty, it is restored to the original pointer. Every time you point to a key-value pair, you can operate on the key-value pair inside foreach.
Then let’s take a look at the traversal of the two-dimensional array:
<?php $arr3=[[3,0,9],[7,8,3],[1,8,2]]; foreach($arr3 as $key=>$value){ foreach($value as $k=>$v){ echo '这是二维数组中索引为:'.$key.'的一维数组的第'.$k.'个,值为:'.$v.'<br>'; } echo "<br>"; } ?>
输出: 这是二维数组中索引为:0的一维数组的第0个,值为:3 这是二维数组中索引为:0的一维数组的第1个,值为:0 这是二维数组中索引为:0的一维数组的第2个,值为:9 这是二维数组中索引为:1的一维数组的第0个,值为:7 这是二维数组中索引为:1的一维数组的第1个,值为:8 这是二维数组中索引为:1的一维数组的第2个,值为:3 这是二维数组中索引为:2的一维数组的第0个,值为:1 这是二维数组中索引为:2的一维数组的第1个,值为:8 这是二维数组中索引为:2的一维数组的第2个,值为:2
In fact, the traversal of the two-dimensional array is to treat the array inside the two-dimensional array as a Variable, use foreach() again to traverse the array we treat as a variable.
Recommended: 《2021 PHP interview questions summary (collection)》《php video tutorial》
The above is the detailed content of How to understand foreach traversing a two-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!

一、Iterator和foreach的区别多态差别(foreach底层就是Iterator)Iterator是一个接口类型,他不关心集合或者数组的类型;for和foreach都需要先知道集合的类型,甚至是集合内元素的类型;1.为啥说foreach底层就是Iterator编写的代码:反编译代码:二、foreach与iterator时remove的区别先来看阿里java开发手册但1的时候不会报错,2的时候就会报错(java.util.ConcurrentModificationException)首

php判断foreach循环到第几个的步骤:1、创建一个“$fruits”的数组;2、创建一个计数器变量“$counter”初始值为0;3、使用“foreach”循环遍历数组,并在循环体中增加计数器变量的值,再输出每个元素和它们的索引;4、在“foreach”循环体外输出计数器变量的值,以确认循环到了第几个元素。

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
