基于列宽度数组PHP构建网格行

Ben*_*enn 2 php arrays

我有一个列宽+内容的数组,我需要根据它们处理和构建行.

$content = array(
    '100',
    '80',
    '25',
    '25',
    '25',
    '25',
    '50',
    '50',
    '-1',
    '33.333333333333',
    '33.333333333333',
    '33.333333333333',
    '50',
    '50',
    '100',
    '16.666666666667',
    '-1'
);
Run Code Online (Sandbox Code Playgroud)

-1表示这不是列,而是文本或短代码,不应该包含在行中.

从上面的数组中所需处理的数组应该是

$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'
);
Run Code Online (Sandbox Code Playgroud)

我已经尝试了一个带有启动累加器0的循环,我从循环中添加了宽度但是只是错误.

任何帮助表示赞赏.

fed*_*sas 5

映射阵列.

$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;
}
Run Code Online (Sandbox Code Playgroud)