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

WBOY
Release: 2016-05-16 09:00:20
Original
1857 people have browsed it

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);
?>

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

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.

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!