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

高洛峰
Release: 2023-03-05 12:56:01
Original
1350 people have browsed it

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);
?>
Copy after login

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
    )
)
Copy after login

Key code:

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

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!

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