0

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++;
     }
  }

kabus
  • 899
  • 1
  • 11
  • 20
  • So you want to split all your array entries into individual parts at the `*` first, yes? – CBroe Sep 09 '22 at 13:06
  • To be honest it looks more like this should be handled by your output formatting (CSS) rather than by your PHP manually editing the data string(s). – Martin Sep 09 '22 at 13:07
  • [How can I sort arrays and data in PHP?](https://stackoverflow.com/q/17364127/1427878) should really tell you most of what you need to know already. For the _"based on strlen that is closed to the $limit"_ part - take the absolute value of the difference of the two, https://www.php.net/manual/en/function.abs.php – CBroe Sep 09 '22 at 13:08
  • Or are you trying to create _new_ combinations out of the parts, so that two (or more) come closest to your limit? That'd be more or less a variation of the _knapsack problem_ then, I'd say. – CBroe Sep 09 '22 at 13:09
  • i think you should create an array of all the possible combinations then sort them – soma Sep 09 '22 at 13:20
  • Does this answer your question? [How can I sort arrays and data in PHP?](https://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php) – Rohit Gupta Sep 09 '22 at 21:59

1 Answers1

1

Hope this helps, you should add the last case and try to improve complexity

<?php
function cmp($a, $b)
{
    return $a["key"] >= $b["key"];
}

$limit = 29;

$arr = array('Isofix', 'Parkinghelp', 'Rainsensor', 'Led light', 'Hill Start Assist', 'Dynamic brake light', 'Cornering Brake Control');
$result=[];
$arrayToCheckin=[];
for ($i=0; $i < count($arr); $i++) {
    for ($j=$i+1; $j < count($arr); $j++) {
        $key = abs(strlen($arr[$i]) + strlen($arr[$j]) - $limit);
            $arrayToCheckin[] = [
                'key'=>$key,
                'elem1'=>$arr[$i],
                'elem2'=>$arr[$j]
                ];
    }
}
usort($arrayToCheckin, "cmp");
$control = [];
foreach ($arrayToCheckin as $val) {
    if(!in_array($val['elem1'], $control) && !in_array($val['elem2'], $control)) {
        $result[] =  $val['elem1'].'*'.$val['elem2'];   
        $control[] = $val['elem1'];
        $control[] = $val['elem2'];
    }
}
var_export($result);

output

array (
  0 => 'Isofix*Cornering Brake Control',
  1 => 'Rainsensor*Dynamic brake light',
  2 => 'Parkinghelp*Hill Start Assist',
)
    
soma
  • 176
  • 9