PHPBuilder - Create Multidimensional Array



RSS Twitter
Snippets Algorithms

Create Multidimensional Array

by: smirniotopoulos
|
September 4, 2000

Version: 1.1

Type: Function

Category: Algorithms

License: Other

Description: A function that takes as input a character-delimited string and returns a multidimensional array.



<? // Begin PHP processing;
echo "<body bgcolor=gray>";
///////////////////////////
// Function for reading a single dimension (column) array into a multiple dimension array
// Copyright 2000 by J.G. Smirniotopoulos
// Made available for non-commercial public use, provided this copyright attribution is intact
// Requires:
//	$cols = number of columns for output array
// 	$buffers = single column array with cell values separated by commas ( , )
//
// NOTE: You can clearly organize your data into columns this way;
// The first row (0) can optionally be used for column headings;
// Each entry (line) must end with a comma ( , );
//
// SAMPLE Array using comma [,] as separator;
//
$buffers="
Numbers, Roman<br>Numerals, English, Greek,
 1, I, one, ena,
 2, II, two, dio,
 3, III, three, tria,
 4, IV, four, tecera
";// end string;

$buff = explode (",", $buffers);

////////////////////////////
// Create Multidimensional Array
//
// requires array $buff;
// requires number of columns for output table $cols;

$cols=4;// define number of columns here;

function make_table($cols,$buff){

$rows=((count($buff))/$cols); // calculate number of rows;
echo "Total Items = ".count($buff)."<br>";
echo "Columns = ".$cols."<br>";
echo "Total Rows = ".$rows."<br>";
echo "<table valign=top align=center border=2 bgcolor=blue>"; 
$item=0;
while ($item < (count($buff))):
for ($row=0; $row < $rows; $row++){
		if (($row%2)==0){echo "<tr bgcolor=orange>";}else{echo "<tr bgcolor=yellow>";};
		if ($row>0){
		echo "<td>Row Number ". ($row)." </td>";
		}else{
		echo "<td><b>Column Titles ==></b></td>";
		}
		for ($col=0; $col < $cols; $col++){
			$myarray[$row][$col]=$buff[$item];
			echo "<td>".$myarray[$row][$col] ;// optional display;
//			if ((($col+1)%$cols)!=0) { echo " - is associated with - ";};// optional display;
			echo "</td>";
			$item++;// loop items;
		}// loop columns;
		echo "<br>";
echo "</tr>";
}// loop rows;
endwhile ; 
echo "</table>";
}// end function make_table;

make_table(4,$buff);// number of columns, input array;
//////////////////////////
// End PHP processing;
?>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
smirniotopoulos

Comment:



Comment:

(Maximum characters: 1200). You have characters left.