How to implement two-dimensional array rotation in php

藏色散人
Release: 2023-03-14 13:08:02
Original
2048 people have browsed it

php method to implement two-dimensional array rotation: 1. Create a PHP sample file; 2. Define a two-dimensional array; 3. Define an array to place the transposed data; 4. Implement it through for loop traversal Just transpose the two-dimensional array.

How to implement two-dimensional array rotation in php

#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.

How to implement two-dimensional array rotation in php?

PHP two-dimensional array rectangular transposition example

<?php
   
  //二维数组转置
  //定义一个二维数组
 
  $arr =array(array(1,2,3),array(4,5,6));
 
  //定义一个数组来放置转置的数据
  $arr1=array();
 
  //转置前遍历
  echo "转置前:<br/>";
  for($i=0;$i<count($arr);$i++){
     
    for($j=0;$j<count($arr[$i]);$j++){
     
      echo $arr[$i][$j];
       
    }
    echo "<br/>";
  }
 
  echo "转置前后:<br/>";
  //确定装置后的行数
  for($j=0;$j<count($arr[0]);$j++){
    $arr1[$j]=array();
  }
  for($i=0;$i<count($arr);$i++){
     
    for($j=0;$j<count($arr[$i]);$j++){
     
      $arr1[$j][$i]= $arr[$i][$j];
      //echo $arr;
    }
  }
 
  //转置后遍历:
  for($k=0;$k<count($arr1);$k++){
     
    for($l=0;$l<count($arr1[$k]);$l++){
     
      echo $arr1[$k][$l];
    }
    echo "<br/>";
  }
?>
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to implement two-dimensional array rotation in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!