Skip to content

Instantly share code, notes, and snippets.

@hugeval
Created June 11, 2019 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugeval/407410cb1e5038cc652fd2ba9ee37353 to your computer and use it in GitHub Desktop.
Save hugeval/407410cb1e5038cc652fd2ba9ee37353 to your computer and use it in GitHub Desktop.
<?php
//$pattern = '/^(.*)\[(p|m|v)_(.*)\]/i';
//preg_match($pattern, '331-60905/5106[p_0].JPG', $matches);
//
//var_dump($matches);
$orig = [2.2, 2, 0.7];
$sQty = 4.2;
$new = array_fill(0, count($orig), 0);
$compare = 0;
foreach ($orig as $key => $item) {
$compare += $item;
if ($compare <= $sQty) {
$new[$key] = $item;
} else {
$diff = $compare - $sQty;
$new[$key] = $item - $diff;
break;
}
}
var_dump($new);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment