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

PHP converts a one-dimensional array into a two-dimensional array consisting of every 3 consecutive values

高洛峰
高洛峰Original
2017-02-06 15:54:251366browse

The example of 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(&#39;aa&#39;,&#39;bb&#39;,&#39;cc&#39;,&#39;dd&#39;,&#39;ee&#39;,&#39;ff&#39;,&#39;gg&#39;,&#39;hh&#39;,&#39;ii&#39;);
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个一组

I hope this article will be helpful to everyone in PHP programming.

For more articles related to PHP converting a one-dimensional array into a two-dimensional array composed of three consecutive values, please pay attention to 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