# two-dimensional factorial (sorta) function

By mike
on March 28, 2002

Version: 1.11

Type: Function

Category: Algorithms

Description: given a 2d array, function will return a 1d array containing strings for all possible column combinations of 2d array. tiny bug that results in an incomplete sentence being returned when iterating down left most column, but all in all, works great! took a while to figure out!

```//--------------------------------------------------------------------
/**
* recursive function that takes in a 2d array and returns 1d array containing all possible
* column-row combinations.
*
* @param a    - 2d array
* @param col  - column value to work with.  original function call should have this at 0
* @param row  - row value to work with.  oringinal function call should have this at 0
* @param tempSentence  - temp sentence that contains sentence fragments before they are finished and
*                      - entered into \$sentence
* @returns    1d array
*
* example call: \$returnArray = doBuilding(\$wordArray);
* example use: array--
*                this function works
*                ths  fnctn    wrks
*                thz  fctn     wrx
*                thiz          wks
*
*              returns--
*                thisfunctionworks
*                thisfunctionwrks
*                thisfunctionwrx
*                thisfunctionwks
*                thisfnctnworks
*                thisfnctnwrks
*                etc...
*/
function doBuilding(\$a,\$col=0,\$row=0,\$tempSentence="")
{  //if there are more columns to the right, add this column to temp sentence and move on to
//next column
if (\$col+1 < count(\$a))
{  //for each row in this column, add data to temp array and call function with next right row
for (\$i=0; \$i < count(\$a[\$col]); \$i++)
{  \$temp = \$tempSentence . \$a[\$col][\$i];
\$temp2 = \$this->doBuilding(\$a,\$col+1,\$i,\$temp);
\$sentence = array_merge(\$sentence,\$temp2);
}

//else add all rows from this column to end of temp sentence and return array
}else
{  for (\$i=0; \$i < count(\$a[\$col]); \$i++)
\$tempArray[\$i] = \$tempSentence . \$a[\$col][\$i];
return \$tempArray;
}

return \$sentence;
}```
﻿