<?php
$records = [
['1', '2'],
['3', '9'],
['12', '8' ],
['20', '2' ],
['22', '15' ],
['37', '16' ],
['53', '16' ],
['69', '16' ],
['85', '1' ],
['86', '4' ],
['90', '4' ],
['94', '4' ],
['98', '4' ],
['102', '4', '5' ],
['106+X', 'X'],
['106+X', '5', '10'],
['111+X', 'Y'],
['111+X+Y', '7', '20'],
['118+X+Y', 'Z'],
];
$X = 10;
$Y = 15;
$Z = 5;
foreach ($records as $posLen) {
$pos = $posLen[0];
$len = $posLen[1];
$val = $posLen[2];
// if $len is a character, then it represents a variable.
if (!is_numeric($len)) {
$len = $$len;
}
$finalPos = 0;
$posArray = explode('+', $pos);
foreach($posArray as $posItem) {
if (is_numeric($posItem)) {
$finalPos += $posItem;
} else {
$finalPos += $$posItem;
}
}
echo "$finalPos, $len" . PHP_EOL;
}
preferences:
61.25 ms | 402 KiB | 5 Q