Need some help. I want to group my array based on strlen that is closed to the $limit and sort on total length
My array looks likes this:
(
[0] => Isofix
[1] => Parkinghelp
[2] => Rainsensor
[3] => Led light
[4] => Hill Start Assist
[5] => Dynamic brake light
[6] => Cornering Brake Control
)
so for example i want to sum the best option that is close to 29.
So ideally this is the combinations that i want to find
Cornering Brake Control(23) + Isofix(6) = length 29
Dynamic brake light(19) + Rainsensor(10) = 29
Hill Start Assist(17) + Parkinghelp(11) = 28
Led light = 90
My final array should look something like this:
Array
(
[0] => Cornering Brake * Control Isofix
[1] => Dynamic brake light * Rainsensor
[2] => Hill Start Assist * Parkinghelp
[3] => Led light
)
// code
$limit = 29;
$arr = array('Isofix', 'Parkinghelp', 'Rainsensor', 'Led light', 'Hill Start Assist', 'Dynamic brake light', 'Cornering Brake Control');
foreach ($arr as $key => $value) {
echo $value . ' - ' . strlen($value);
echo "\n";
}
// this is what i have so far. But it's not checking every possibility
$limit = 29;
$result9009 = array('');
$cur_key = 0;
foreach ($arr as $word) {
if (strlen($result9009[$cur_key]) + strlen($word) <= $limit) {
$result9009[$cur_key] .= ' * ' . $word;
} else {
$result9009[] = $word;
$cur_key++;
}
}