Revision: 59930
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at October 10, 2012 11:47 by kosinix
Initial Code
<?php $test = array(1,2,3,4,5,6,7,8,9); function array_2d($array, $col_count=2){ $result = false; if(!empty($array) && is_array($array)){ $row_count = ceil( count($array) / $col_count); $pointer = 0; for($row=0; $row < $row_count; $row++) { for($col=0; $col < $col_count; ++$col){ if(isset($array[$pointer])) { $result[$row][$col] = $array[$pointer]; $pointer++; } } } } return $result; } $result = array_2d($test, 3); echo '<pre>'.print_r($result, 1).'</pre>'; ?>
Initial URL
Initial Description
Function to convert a one dimensional array to two dimensional with option to specify how many columns. Returns two dimensional array on success or false on fail.
Initial Title
PHP Convert Single Dimensional Array to Two Dimensional
Initial Tags
array
Initial Language
PHP