I did this
while....//to get dynamic value of $data[18]
One of the value is
$data[18] = '256000000000;2001111162880;2001111162880;2001111162880;10000831348736;1800360124416;256000000000';
$result = [];
foreach(explode("n", $data[18]) as $str){
$result[] = implode("GB/", array_map(fn($v) => floor((int)$v / 1e9), explode(";", $str))). 'GB';
}
$data[18] = implode("n", $result); //GB
$tb = ((int)substr($data[18],0,-2) / 1000);
$data[18] = preg_replace('~bw{6,}b~', $tb . 'TB', $data[18]); //TB
echo $data[18];
And gets
256GB/0.256TB/0.256TB/0.256TB/0.256TB/0.256TB/256GB
What I expected was (I know my code was wrong to begin with)
256GB/2TB/2TB/2TB/10TB/1.8TB/256GB
However, the final result that I actually want is (the order doesn’t matter)
2 x 256GB/3 x 2TB/10TB/1.8TB
Sometimes value of $data[18]
is empty or only 1
. eg. $data[18] = '1800360124416';
In that case I want an empty result or just 1.8TB (without 1 x
)
2
Answers
I think using something like this is a better way to aggregate the data:
There a many human readable functions to convert bytes to a human readable format. It’s not clear to me how you want the data converted though. From above example passing
$value
to any of those functions should achieve your goal.https://3v4l.org/iQrC5