Mihsak Yamato
Mihsak Yamato

Reputation: 98

Combine Words and Numbers in Arrays using PHP

I want to combine the two arrays in my results
MY CODE

   <?php 

   /* Designated level for each exp
   Level 2 - 23 exp
   Level 3 - 34 exp
   Level 4 - 45 exp
   Level 5 - 56 exp
   Level 6 - 68 exp
   Level 7 - 79 exp
   Level 8 - 90 exp
   Level 9 - 101 exp
   Level 10 - 112 exp
   Level 11 - 123 exp
   Level 12 - 134 exp
   Level 13 - 145 exp
   Level 14 - 156 exp
   Level 15 - 168 exp
   Level 16 - 179 exp
   */

   $limit =  100000-99318;  

    // Level
   $arrlevel = array ('Level 2','Level 3','Level 4','Level 5','Level 6','Level 7','Level 8','Level 9','Level 10','Level 11','Level 12','Level 13','Level 14','Level 15','Level 16');

   // Exp
   $array = array (23,34,45,56,68,79,90,101,112,123,134,145,156,168,179);

   $array = array_filter($array, function($var) use ($limit) {
   return ($var <= $limit);
   });

  $num = count($array); 
  $total = pow(2, $num);
   $out = array();

   for ($i = 0; $i < $total; $i++) { 

   $comb = array();
     for ($j = 0; $j < $num; $j++) { 
       // is bit $j set in $i? 
    if (pow(2, $j) & $i){
      $comb[] = $array[$j];
           }      
      } 

       if (array_sum($comb) == $limit)
      {
        $out[] = $comb;
       }
      }

      array_multisort(array_map('count', $out), SORT_ASC, $out);

       $out = array_unique($out, SORT_REGULAR);
     $m = 1;
    foreach($out as $result) 

     echo "<b>Possible Answer ". $m++. " : </b> " .implode(', ', $result)." 
  <br><br>";    
        ?>

Output:

Possible Answer 1 :
23, 34, 45, 68, 79, 90, 112, 179

Possible Answer 2 :
23, 34, 45, 68, 79, 90, 123, 168

Possible Answer 3 :
23, 34, 45, 68, 79, 101, 112, 168


I want the ouput like this

Possible Answer 1 :
Level 2 - 23 | Level 3 - 34 | Level 4 - 45 | Level 6 - 68 | Level 7 - 79 | Level 8 - 90 | Level 10 - 112 | Level 16 - 179

-----------------------------------------
I want to combine the two arrays
(This program is about finding all combination to reach the result [Subset sum program])

Upvotes: 0

Views: 109

Answers (2)

Just do it and do sort

$combine=array_combine($arrlevel,$array);

Upvotes: 0

The fourth bird
The fourth bird

Reputation: 163207

You might use a mapper for the entries of Level n and the experience.

Then for the $result array in the loop you could use array_map and for each item that you are mapping use the value as the key for the $mapper array and implode using |

That will format all the possible answers in this format:

Possible Answer 1 :
Level 3 - 34 | Level 13 - 145 | Level 14 - 156 | Level 15 - 168 | Level 16 - 179 

For example

$mapper = [
    23 => "Level 2",
    34 => "Level 3",
    45 => "Level 4",
    56 => "Level 5",
    68 => "Level 6",
    79 => "Level 7",
    90 => "Level 8",
    101 => "Level 9",
    112 => "Level 10",
    123 => "Level 11",
    134 => "Level 12",
    145 => "Level 13",
    156 => "Level 14",
    168 => "Level 15",
    179 => "Level 16",
];

foreach($out as $result)
    echo "<b>Possible Answer ". $m++. " : </b> " .implode(' | ', array_map(function($x) use ($mapper) {
            return $mapper[$x] . " - " . $x;
        }, $result))." 
  <br><br>";

Php demo

Upvotes: 1

Related Questions