Adam Rodriguez
Adam Rodriguez

Reputation: 1856

I want to define the number of columns instead of rows

I want to be able to define the number of columns, however, this does not define columns. You will notice when you see the output that it changes the number of rows instead of the number of columns.

I have documented as thorough as possible, thanks for all your help!. First time posting so bear with me if I made a mistake :)

public function SplitIntoColumns(array $arrayTranspose=array(), $no_of_cols=3,$min=0) {
    $result = '<div class="column">'."\n";
    // The 2D array
    $colArr = array();
    // The tmpArray
    $tmpArray = array();
    if (count($arrayTranspose) <= $min) {
        $result .= implode("\n", $arrayTranspose);
    }
    else {
        // Set size of the array    
        $cnt = intval(floor(count($arrayTranspose)));
        $row = 0;
        $col = 0;
        // Create Multidimensional array from Single dimensional array
        foreach($arrayTranspose as $key => $val){
            $colArr[$col][$row] = $arrayTranspose[$key];
            // Store the data from the array
            $result .= $colArr[$col][$row]." ";
            $col++;
            // If columns are equal to the set value then start
            // From the beginning on a new row
            if($col == $no_of_cols) {
                $col = 0;
                $row++;
                // create columns
                $result .= '</div><div class="column">'."\n";   
            }
        }
    }
    // This says numberOfRows, but it is actually the number of columns
    $numberOfRows = intval((floor($cnt) / $no_of_cols));
    // A loop to transpose into columns
    for($i = 0; $i <= $no_of_cols; $i++) {
        for($j = 0; $j <= $numberOfRows; $j++){
            $tmpArray[$j][$i] = $colArr[$i][$j];
            $result2 .= $tmpArray[$j][$i].' ';  
        }
        $result2 .= '</div><div class="column">'."\n";
    }
    // return stuff
    $result2 .= "</div> <!-- /.column -->\n";
    return $result2;
}
}
$Util = new SplitColumns();
$content = array('Lorem ipsum','elit sed do','labore et dolore', 'dolor sit amet', 'eiusmod tempor', 'magna aliqua',  'consectetur adipisicing', 'incididunt ut');
// This is where you can define the number of columns you want
// However, this does not define columns.  You will notice when
// You see the output that it changes the number of rows
echo $Util->SplitIntoColumns($content, 4);

The output for this:

Lorem ipsum eiusmod tempor
elit sed do magna aliqua
labore et dolore consectetur adipisicing
dolor sit amet incididunt ut

The desired output should be:

Lorem ipsum labore et dolore eiusmod tempor consectetur adipisicing
elit sed do dolor sit amet magna aliqua incididunt ut

Upvotes: 1

Views: 152

Answers (2)

Nir Alfasi
Nir Alfasi

Reputation: 53535

You can get the expected output in a simpler way:

$content = array('Lorem ipsum','elit sed do','labore et dolore', 'dolor sit amet', 'eiusmod tempor', 'magna aliqua',  'consectetur adipisicing', 'incididunt ut');    
for($i = 0; $i<count($content); $i++){
    if($i % 2 == 0){
        $col1 .= $content[$i] . ' ';
    }
    else{
        $col2 .= $content[$i] . ' ';
    }
}    
echo $col1 . "\n" . $col2;

Output:

Lorem ipsum labore et dolore eiusmod tempor consectetur adipisicing
elit sed do dolor sit amet magna aliqua incididunt ut 

Upvotes: 0

orourkek
orourkek

Reputation: 2101

You're overcomplicating things! Use array_chunk() combined with some simple math to format the data correctly:

function splitIntoColumns($array, $columns=3)
{
    $numberOfRows = ceil(count($array) / $columns);
    return array_chunk($array, $numberOfRows);
}

This leaves you with an array of columns - $returnValue[0] is an array of the elements in the 0th column, and so on up to the column limit. Example output (4 cols):

array(4) {
  [0]=>
  array(2) {
    [0]=>
    string(11) "Lorem ipsum"
    [1]=>
    string(11) "elit sed do"
  }
  [1]=>
  array(2) {
    [0]=>
    string(16) "labore et dolore"
    [1]=>
    string(14) "dolor sit amet"
  }
  [2]=>
  array(2) {
    [0]=>
    string(14) "eiusmod tempor"
    [1]=>
    string(12) "magna aliqua"
  }
  [3]=>
  array(2) {
    [0]=>
    string(23) "consectetur adipisicing"
    [1]=>
    string(13) "incididunt ut"
  }
}

From there the addition of HTML should be fairly simple, and could be accomplished in many ways, for example:

$result = splitIntoColumns($inputArray, 3);

foreach($result as $column => $columnMembers)
{
    echo '<div class="column">';
    foreach($columnMembers as $key => $val)
    {
        echo $val; 
        //or whatever else you want to do in here
    }
    echo '</div> <!-- /column -->';
}

edit: Example with html output

Upvotes: 1

Related Questions