search
HomeBackend DevelopmentPHP ProblemHow to understand foreach traversing a two-dimensional array in php

phpIt 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(&#39;a&#39;=>&#39;php&#39;,&#39;c&#39;=>&#39;.cn&#39;);
foreach($arr as $key=>$value){
    echo $key.&#39;=>&#39;.$value."<br>";
}
输出:a=>php
      c=>.cn

2. Need to operate on the keys and values ​​of the array Operation

<?php
$arr2=array(&#39;d&#39;=>&#39;ok&#39;,&#39;b&#39;=>&#39;oya&#39;);
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 &#39;这是二维数组中索引为:&#39;.$key.&#39;的一维数组的第&#39;.$k.&#39;个,值为:&#39;.$v.&#39;<br>&#39;;
    }
    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!

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
Java ArrayList遍历时使用foreach和iterator删除元素的区别是什么?Java ArrayList遍历时使用foreach和iterator删除元素的区别是什么?Apr 27, 2023 pm 03:40 PM

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

php如何判断foreach循环到第几个php如何判断foreach循环到第几个Jul 10, 2023 pm 02:18 PM

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

php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

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

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

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

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

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

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

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

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

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

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

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

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尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment