Jump to content

How to output array instead of a 'loop' echo return from a function?


shamuraq
Go to solution Solved by Barand,

Recommended Posts

I have a function randomly take 4 unique numbers from an array and multiply them. Problem with the script below is it only print out the products. I would like for each set of 4 numbers and its product to be in its own array and all the arrays into a main array, forming a 2 dimensional array. My current script:

//Pruning outcome from controlled lists of Prime Numbers
$primes = array(2, 3, 5, 7, 11, 13, 17, 19, 23);


function getAllCombinations($arr, $n, $selected = array(), $startIndex = 0) {
  if ($n == 0) {
    $product = 1;
    foreach ($selected as $prime) {
      $pr[] = $prime;
      $product *= $prime;
      $pr[] = $prime;
    }
    echo "Product: $product\n";
    return;
  }

  for ($i = $startIndex; $i < count($arr); $i++) {
    $selected[] = $arr[$i];
    getAllCombinations($arr, $n - 1, $selected, $i + 1);
    array_pop($selected); // Backtrack and remove the element for next iteration
  }
}  

getAllCombinations($primes, 4);

The output:

Product: 210 Product: 330 Product: 390 Product: 510, etc.

The preferred method is a 2 dimensional array as such:

[0][0] =>2 [0][1] =>3 [0][2]=>5 [0][3]=>7 and the product would be in [0][4] =>210

[1][0] =>2 [1][1] =>3 [1][2]=>5 [1][3]=>11 and the product would be in [1][4] =>330

etc.

Any pointers is greatly appreciated.

Link to comment
Share on other sites

  • Solution

You've done the hard work already. Instead of calculating the product, store the selected array.

<?php
$primes = array(2, 3, 5, 7, 11, 13, 17, 19, 23);
$combos = [];

function getAllCombinations($arr, $n, &$combos, $selected = array(), $startIndex = 0) {
  if ($n == 0) {
      $combos[] = $selected;
//    $product = 1;
//    foreach ($selected as $prime) {
//      $pr[] = $prime;
//      $product *= $prime;
//      $pr[] = $prime;
//    }
//    echo "Product: $product\n";
    return;
  }

  for ($i = $startIndex; $i < count($arr); $i++) {
    $selected[] = $arr[$i];
    getAllCombinations($arr, $n - 1, $combos, $selected, $i + 1);
    array_pop($selected); // Backtrack and remove the element for next iteration
  }
}  

getAllCombinations($primes, 4, $combos);

echo '<pre>';
foreach ($combos as $com)  {
    printf("%-35s = %5d<br>", join(' &times; ', $com), array_product($com));    // output numbers and product
}

?>

giving

2 × 3 × 5 × 7     =   210
2 × 3 × 5 × 11    =   330
2 × 3 × 5 × 13    =   390
2 × 3 × 5 × 17    =   510
2 × 3 × 5 × 19    =   570
2 × 3 × 5 × 23    =   690
2 × 3 × 7 × 11    =   462
2 × 3 × 7 × 13    =   546
2 × 3 × 7 × 17    =   714
2 × 3 × 7 × 19    =   798
2 × 3 × 7 × 23    =   966
2 × 3 × 11 × 13   =   858
2 × 3 × 11 × 17   =  1122
2 × 3 × 11 × 19   =  1254
2 × 3 × 11 × 23   =  1518
2 × 3 × 13 × 17   =  1326
2 × 3 × 13 × 19   =  1482
2 × 3 × 13 × 23   =  1794
2 × 3 × 17 × 19   =  1938
2 × 3 × 17 × 23   =  2346
2 × 3 × 19 × 23   =  2622
2 × 5 × 7 × 11    =   770
2 × 5 × 7 × 13    =   910
.
.
5 × 17 × 19 × 23  = 37145
7 × 11 × 13 × 17  = 17017
7 × 11 × 13 × 19  = 19019
7 × 11 × 13 × 23  = 23023
7 × 11 × 17 × 19  = 24871
7 × 11 × 17 × 23  = 30107
7 × 11 × 19 × 23  = 33649
7 × 13 × 17 × 19  = 29393
7 × 13 × 17 × 23  = 35581
7 × 13 × 19 × 23  = 39767
7 × 17 × 19 × 23  = 52003
11 × 13 × 17 × 19 = 46189
11 × 13 × 17 × 23 = 55913
11 × 13 × 19 × 23 = 62491
11 × 17 × 19 × 23 = 81719
13 × 17 × 19 × 23 = 96577

 

  • Great Answer 2
Link to comment
Share on other sites

If I understand correctly, you are only lacking the visual display of the array elements being multiplied.

Why not echo them accordingly. Or if you want the actual array positions then maybe adapt something or utilize var_dump.

Edited by phppup
Typos
Link to comment
Share on other sites

11 hours ago, Barand said:

You've done the hard work already. Instead of calculating the product, store the selected array.

<?php
$primes = array(2, 3, 5, 7, 11, 13, 17, 19, 23);
$combos = [];

function getAllCombinations($arr, $n, &$combos, $selected = array(), $startIndex = 0) {
  if ($n == 0) {
      $combos[] = $selected;
//    $product = 1;
//    foreach ($selected as $prime) {
//      $pr[] = $prime;
//      $product *= $prime;
//      $pr[] = $prime;
//    }
//    echo "Product: $product\n";
    return;
  }

  for ($i = $startIndex; $i < count($arr); $i++) {
    $selected[] = $arr[$i];
    getAllCombinations($arr, $n - 1, $combos, $selected, $i + 1);
    array_pop($selected); // Backtrack and remove the element for next iteration
  }
}  

getAllCombinations($primes, 4, $combos);

echo '<pre>';
foreach ($combos as $com)  {
    printf("%-35s = %5d<br>", join(' &times; ', $com), array_product($com));    // output numbers and product
}

?>

giving

2 × 3 × 5 × 7     =   210
2 × 3 × 5 × 11    =   330
2 × 3 × 5 × 13    =   390
2 × 3 × 5 × 17    =   510
2 × 3 × 5 × 19    =   570
2 × 3 × 5 × 23    =   690
2 × 3 × 7 × 11    =   462
2 × 3 × 7 × 13    =   546
2 × 3 × 7 × 17    =   714
2 × 3 × 7 × 19    =   798
2 × 3 × 7 × 23    =   966
2 × 3 × 11 × 13   =   858
2 × 3 × 11 × 17   =  1122
2 × 3 × 11 × 19   =  1254
2 × 3 × 11 × 23   =  1518
2 × 3 × 13 × 17   =  1326
2 × 3 × 13 × 19   =  1482
2 × 3 × 13 × 23   =  1794
2 × 3 × 17 × 19   =  1938
2 × 3 × 17 × 23   =  2346
2 × 3 × 19 × 23   =  2622
2 × 5 × 7 × 11    =   770
2 × 5 × 7 × 13    =   910
.
.
5 × 17 × 19 × 23  = 37145
7 × 11 × 13 × 17  = 17017
7 × 11 × 13 × 19  = 19019
7 × 11 × 13 × 23  = 23023
7 × 11 × 17 × 19  = 24871
7 × 11 × 17 × 23  = 30107
7 × 11 × 19 × 23  = 33649
7 × 13 × 17 × 19  = 29393
7 × 13 × 17 × 23  = 35581
7 × 13 × 19 × 23  = 39767
7 × 17 × 19 × 23  = 52003
11 × 13 × 17 × 19 = 46189
11 × 13 × 17 × 23 = 55913
11 × 13 × 19 × 23 = 62491
11 × 17 × 19 × 23 = 81719
13 × 17 × 19 × 23 = 96577

 

Tes... Thats exactly what i wanted. But can you explain the breakdown of "%-35 = %5d <br>"?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.