Home  >  Article  >  Backend Development  >  How to implement two-dimensional array rotation in php

How to implement two-dimensional array rotation in php

藏色散人
藏色散人Original
2021-12-15 09:30:332033browse

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/>";
  }
?>

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!

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