2

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.

2
  • 1
    Why 16.666666666667 doesn't have a [row] prepended to it? Commented Mar 30, 2017 at 19:31
  • @Duvdevan , fixed , it should be as you assumed Commented Mar 30, 2017 at 19:35

2 Answers 2

5

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;
}
Sign up to request clarification or add additional context in comments.

5 Comments

excellent but on my end it is still wrapping the -1 prntscr.com/eqh6hu try moving it to the end of the array
@Benn is -1 a string or a number? I've updated the response. Try again.
@feideisas darn , is there any way to do this via loop , I just figured that I must extend that array to be array of arrays like [key]=> array('width'=>'80','content'=>'the content ')
sorry bud but is failing on this one prntscr.com/eqjdqb here is test eval.in/764902 keys 7,8,9 should fit in same row
@Benn I don't understand your questions, sorry.
1

$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);

3 Comments

almost , is failing , eval.in/764905, key 15 should be wrapped , but you are right about calculating the sum
check now, was fixed
looks ok , but why are you closing previous row on next key ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.