Home Backend Development PHP Tutorial PHP implements two-dimensional array assignment and traversal functions

PHP implements two-dimensional array assignment and traversal functions

May 17, 2018 pm 06:13 PM
php Assignment Traverse

This article mainly introduces the simple implementation of two-dimensional array assignment and traversal functions in PHP, involving the simple assignment, traversal, operation, reading and other operation techniques of PHP arrays. Friends who need it can refer to it

The example in this article describes the simple implementation of two-dimensional array assignment and traversal functions in PHP. Share it with everyone for your reference, the details are as follows:

Example 1:

<?php
  $loptop1[&#39;lid&#39;] = 1000;
  $loptop1[&#39;pic&#39;] = &#39;img/1.png&#39;;
  $loptop1[&#39;title&#39;] = &#39;L1&#39;;
  $loptop1[&#39;price&#39;] = 5000;
  $loptop1[&#39;isOnSale&#39;] = 1;
  $loptop1[&#39;shelfTime&#39;] = 1234556;
  $loptop2[&#39;lid&#39;] = 1001;
  $loptop2[&#39;pic&#39;] = &#39;img/2.png&#39;;
  $loptop2[&#39;title&#39;] = &#39;L2&#39;;
  $loptop2[&#39;price&#39;] = 5000;
  $loptop2[&#39;isOnSale&#39;] = 1;
  $loptop2[&#39;shelfTime&#39;] = 123444456;
  $loptop3[&#39;lid&#39;] = 1002;
  $loptop3[&#39;pic&#39;] = &#39;img/3.png&#39;;
  $loptop3[&#39;title&#39;] = &#39;L3&#39;;
  $loptop3[&#39;price&#39;] = 5000;
  $loptop3[&#39;isOnSale&#39;] = 1;
  $loptop3[&#39;shelfTime&#39;] = 1243454556;
  $loptop4[&#39;lid&#39;] = 1003;
  $loptop4[&#39;pic&#39;] = &#39;img/4.png&#39;;
  $loptop4[&#39;title&#39;] = &#39;L4&#39;;
  $loptop4[&#39;price&#39;] = 5000;
  $loptop4[&#39;isOnSale&#39;] = 1;
  $loptop4[&#39;shelfTime&#39;] = 1234364556;
  $loptop[0] = $loptop1;
  $loptop[1] = $loptop2;
  $loptop[2] = $loptop3;
  $loptop[3] = $loptop4;
  for($i=0;$i<count($loptop);$i++){
    //echo "编号:$loptop[$i][lid]"; //错误
    //echo "编号:" . $loptop[$i][&#39;lid&#39;]; //正确,但不推荐
    $tmp = $loptop[$i];
    echo "编号:$tmp[lid]<br/>";
    echo "图片:$tmp[pic]<br/>";
    echo "标题:$tmp[title]<br/>";
    echo "价格:$tmp[price]<br/>";
    echo "是否特价:$tmp[isOnSale]<br/>";
    echo "上架时间:" . date("Y-m-d H:i:s",$tmp[&#39;shelfTime&#39;]) . "<br/>";
  }
?>
Copy after login

Running results:

编号:1000
图片:img/1.png
标题:L1
价格:5000
是否特价:1
上架时间:1970-01-15 06:55:56
编号:1001
图片:img/2.png
标题:L2
价格:5000
是否特价:1
上架时间:1973-11-29 18:07:36
编号:1002
图片:img/3.png
标题:L3
价格:5000
是否特价:1
上架时间:2009-05-27 20:02:36
编号:1003
图片:img/4.png
标题:L4
价格:5000
是否特价:1
上架时间:2009-02-11 15:02:36
Copy after login

Example 2:

<?php
  $stu1[&#39;sid&#39;] = 1000;
  $stu1[&#39;userName&#39;] = "abc1";
  $stu1[&#39;passWord&#39;] = "123456";
  $stu1[&#39;email&#39;] = "2109882885@qq.com";
  $stu1[&#39;tel&#39;] = "15700769164";
  $stu1[&#39;headScu&#39;] = "stu1.png";
  $stu1[&#39;sex&#39;] = "M";
  $stu1[&#39;regTime&#39;] = 1111223435;
  $stu1[&#39;isOnline&#39;] = 1;
  $stu2[&#39;sid&#39;] = 1001;
  $stu2[&#39;userName&#39;] = "abc2";
  $stu2[&#39;passWord&#39;] = "123456";
  $stu2[&#39;email&#39;] = "2109882886@qq.com";
  $stu2[&#39;tel&#39;] = "15700769165";
  $stu2[&#39;headScu&#39;] = "stu2.png";
  $stu2[&#39;sex&#39;] = "M";
  $stu2[&#39;regTime&#39;] = 122435344;
  $stu2[&#39;isOnline&#39;] = 1;
  $stu3[&#39;sid&#39;] = 1002;
  $stu3[&#39;userName&#39;] = "abc3";
  $stu3[&#39;passWord&#39;] = "123456";
  $stu3[&#39;email&#39;] = "2109882887@qq.com";
  $stu3[&#39;tel&#39;] = "15700769166";
  $stu3[&#39;headScu&#39;] = "stu3.png";
  $stu3[&#39;sex&#39;] = "M";
  $stu3[&#39;regTime&#39;] = 3463464567;
  $stu3[&#39;isOnline&#39;] = 0;
  $stu4[&#39;sid&#39;] = 1003;
  $stu4[&#39;userName&#39;] = "abc4";
  $stu4[&#39;passWord&#39;] = "123456";
  $stu4[&#39;email&#39;] = "2109882888@qq.com";
  $stu4[&#39;tel&#39;] = "15700769167";
  $stu4[&#39;headScu&#39;] = "stu4.png";
  $stu4[&#39;sex&#39;] = "F";
  $stu4[&#39;regTime&#39;] = 235234534;
  $stu4[&#39;isOnline&#39;] = 1;
  $stu = [$stu1,$stu2,$stu3,$stu4];
  for($i=0;$i<count($stu);$i++){
    $tmp = $stu[$i];
    echo "编号:$tmp[sid]<br/>";
    echo "用户名:$tmp[userName]<br/>";
    echo "密码:$tmp[passWord]<br/>";
    echo "邮箱:$tmp[email]<br/>";
    echo "手机:$tmp[tel]<br/>";
    echo "头像:$tmp[headScu]<br/>";
    if($tmp[&#39;sex&#39;] == "M"){
      echo "性别:男<br/>";
    }
    if($tmp[&#39;sex&#39;] == "F"){
      echo "性别:女<br/>";
    }
    echo "注册时间:" . date(&#39;Y-m-d H:i:s&#39;,$tmp[&#39;regTime&#39;]) . "<br/>";
    if($tmp[&#39;isOnline&#39;] == 1){
      echo "状态:在线<br/>";
    }
    if($tmp[&#39;isOnline&#39;] == 0){
      echo "状态:不在线<br/>";
    }
  }
?>
Copy after login


Run results:

编号:1000
用户名:abc1
密码:123456
邮箱:2109882885@qq.com
手机:15700769164
头像:stu1.png
性别:男
注册时间:2005-03-19 09:10:35
状态:在线
编号:1001
用户名:abc2
密码:123456
邮箱:2109882886@qq.com
手机:15700769165
头像:stu2.png
性别:男
注册时间:1973-11-18 01:49:04
状态:在线
编号:1002
用户名:abc3
密码:123456
邮箱:2109882887@qq.com
手机:15700769166
头像:stu3.png
性别:男
注册时间:1943-08-27 03:01:11
状态:不在线
编号:1003
用户名:abc4
密码:123456
邮箱:2109882888@qq.com
手机:15700769167
头像:stu4.png
性别:女
注册时间:1977-06-15 14:55:34
状态:在线
Copy after login

Related recommendations:

Detailed explanation of two-dimensional array sorting function array_orderby use cases

PHP Methods and Cases for Implementing Matrix Transposition Operations of Two-Dimensional Arrays

PHP Two-Dimensional Arrays Deduplication or Statistics Based on a Certain Field

The above is the detailed content of PHP implements two-dimensional array assignment and traversal functions. For more information, please follow other related articles on the PHP Chinese website!

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

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

See all articles