Home >Backend Development >PHP Tutorial >PHP converts a one-dimensional array into a two-dimensional array consisting of 3 consecutive values_php tips

PHP converts a one-dimensional array into a two-dimensional array consisting of 3 consecutive values_php tips

WBOY
WBOYOriginal
2016-05-16 09:00:201909browse

the example in this article describes the php implementation of converting a one-dimensional array into a two-dimensional array composed of three consecutive values. share it with everyone for your reference, the details are as follows:

<?php
$aaa = array('aa','bb','cc','dd','ee','ff','gg','hh','ii');
for($i=0;$i<3;$i++)
{
  $bbb[] = array_slice($aaa, $i * 3 ,3);
}
print_r($bbb);
?>

the running results are as follows:

array
(
  [0] => array
    (
      [0] => aa
      [1] => bb
      [2] => cc
    )
  [1] => array
    (
      [0] => dd
      [1] => ee
      [2] => ff
    )
  [2] => array
    (
      [0] => gg
      [1] => hh
      [2] => ii
    )
)

key code:

$bbb[] = array_slice($aaa, $i * 3 ,3);
//3为3个一组,如果是2为2个一组

readers who are interested in more php-related content can check out the special topic of this site: "complete php array (array) operation skills ", "php sorting algorithm summary", " summary of commonly used traversal algorithms and techniques in php", "php data structure and algorithm tutorial", "php programming algorithm summary", "php mathematical operation skills summary a>》,《php regular expression usage summary》, "php operations and operator usage summary", "php string (string) usage summary" and "summary of common database operation skills in php"

i hope this article will be helpful to everyone in php programming.

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