Benn
Benn

Reputation: 5023

Build grid rows based on columns widths array PHP

I have an array of column widths + content that I need to process and build row based on them.

$content = array(
    '100',
    '80',
    '25',
    '25',
    '25',
    '25',
    '50',
    '50',
    '-1',
    '33.333333333333',
    '33.333333333333',
    '33.333333333333',
    '50',
    '50',
    '100',
    '16.666666666667',
    '-1'
);

-1 means that this is not a column , but a text or shortcode and it should not be wrapped in to row.

from the above array desired processed array should be

$content = array(
    '[row]100[/row]',
    '[row]80[/row]',
    '[row]25',
    '25',
    '25',
    '25[/row]',
    '[row]50',
    '50[/row]',
    '-1',
    '[row]33.333333333333',
    '33.333333333333',
    '33.333333333333[/row]',
    '[row]50',
    '50[/row]',
    '[row]100[/row]',
    '[row]16.666666666667[/row]',
    '-1'
);

I have tried a loop with a starting accumulator 0 that I was adding width from loop but is simply buggy.

Any help is appreciated.

Upvotes: 2

Views: 182

Answers (2)

Christian Carrillo
Christian Carrillo

Reputation: 414

$sum = 100;
$new_content = array();
$prev = false;
for($i=0;$i<count($content);$i++){
    $num = $content[$i];
    $diff = $sum-$num;
    echo $num.'=>'.$diff.',';
    if(!$prev && $diff==0){
        $new_content[] = "[row]".$content[$i]."[/row]";
    }

    if($diff==0 && $prev){
        $new_content[] = $content[$i]."[/row]";
        $sum=100;
        $prev = false;
    }

    if($diff>0 && $diff<100 && $prev && $content[$i]!=-1){
        $new_content[] = $content[$i];
        $sum = $diff;
        $prev = true;
    }

    if($diff>0 && $diff<100 && !$prev){
         $new_content[] = "[row]".$content[$i];
         $sum = $diff;
         $prev = true;
    }
    if($diff<0){
        $new_content[] = "[/row][row]".$content[$i];
        $sum = 100-$content[$i];
        $prev = true;
    }
    if($content[$i]==-1 && $prev){
        $new_content[] = "[/row]".$content[$i];
    }else if($content[$i]==-1){
        $new_content[] = $content[$i];
    }

}

print_r($new_content);

Upvotes: 1

fedeisas
fedeisas

Reputation: 2023

Map your array.

$start = [
    '100',
    '80',
    '25',
    // ...removed for brevity
];

function process(array $data) {
    $output = array_map(function ($row, $index) use ($data) {
        if ($row == '-1') {
            return '-1';
        }

        $value = $row;

        $previousIndex = $index - 1;
        $nextIndex = $index + 1;

        // Check previous row
        if (
            // There's a row and it's different
            (isset($data[$previousIndex]) && $data[$previousIndex] != $row)
            ||
            // There's no row, we're on the beggining of the list
            !isset($data[$previousIndex])
        ) {
            $value = '[row]' . $value;
        }

        // Check next row
        if (
            // There is a next row and it's different
            (isset($data[$nextIndex]) && $data[$nextIndex] != $row)
            ||
            // There's no next row, we're on the end of the list
            !isset($data[$nextIndex])
        ) {
            $value = $value . '[/row]';
        }

        return $value;
    }, $data, array_keys($data));

    return $output;
}

Upvotes: 5

Related Questions