PHP find cheapest permutationDetect locale with PHPPHP Random ListFind number matching rangeOptimize PHP...

A bug in Excel? Conditional formatting for marking duplicates also highlights unique value

Plagiarism of code by other PhD student

Why do phishing e-mails use faked e-mail addresses instead of the real one?

When do _WA_Sys_ statistics Get Updated?

What is a term for a function that when called repeatedly, has the same effect as calling once?

Make me a metasequence

Difference between 'stomach' and 'uterus'

How do I deal with being envious of my own players?

Draw bounding region by list of points

PTIJ: What’s wrong with eating meat and couscous?

How can I highlight parts in a screenshot

It doesn't matter the side you see it

Are there other characters in the Star Wars universe who had damaged bodies and needed to wear an outfit like Darth Vader?

How to merge row in the first column in LaTeX

Can the Shape Water Cantrip be used to manipulate blood?

Where is the fallacy here?

Being asked to review a paper in conference one has submitted to

1970s scifi/horror novel where protagonist is used by a crablike creature to feed its larvae, goes mad, and is defeated by retraumatising him

Relationship between the symmetry number of a molecule as used in rotational spectroscopy and point group

Was it really inappropriate to write a pull request for the company I interviewed with?

Using Non-Negative Matrix Factorization (NNMF)

Quitting employee has privileged access to critical information

Must 40/100G uplink ports on a 10G switch be connected to another switch?

Why is it "take a leak?"



PHP find cheapest permutation


Detect locale with PHPPHP Random ListFind number matching rangeOptimize PHP foreach loopsPHP - Adventofcode - Day 1Slider PHP loopPermutation index PythonRecursive autoloader to find PHP filesConfig class in PHPSQL/PHP Laravel - Looping through records to find relationships













0












$begingroup$


I have multiple products that all have sizes and I need to find the cheapest configuration that meets the minimum required size.



For example, John needs a minimum of 10 litres of storage - it can be more, but not less.



There are 2L, 3L, 5L, 8L and 10L options (but this can change).



As an example, it might be cheaper to get:




  • 1x10L container OR

  • 2x5L containers OR

  • 1x2L, 1x3L and 1x5L OR

  • 4x3L (this one is over 10 L, but it is still possible that it will be cheaper)


So far I've tried looping over and over up to 4 times (because typically the maximum requirement will be 40 L), but in some cases I am running out of memory, and it doesn't seem like the most efficient way of doing it.




// Size is in mL

$available_containers = [
[
'id' => 22700,
'price' => 1190,
'size' => 2000,
],
[
'id' => 22701,
'price' => 1245,
'size' => 3000,
],
[
'id' => 22702,
'price' => 1415,
'size' => 4000,
],
[
'id' => 22715,
'price' => 12300,
'size' => 5000,
],
[
'id' => 22706,
'price' => 1740,
'size' => 5000,
],
[
'id' => 22703,
'price' => 1510,
'size' => 5000,
],
[
'id' => 22707,
'price' => 1790,
'size' => 6000,
],
[
'id' => 22704,
'price' => 1770,
'size' => 6000,
],
[
'id' => 22708,
'price' => 2215,
'size' => 7000,
],
[
'id' => 22705,
'price' => 2195,
'size' => 8200,
],
[
'id' => 22709,
'price' => 2660,
'size' => 8200,
],
[
'id' => 22710,
'price' => 2799,
'size' => 10000,
],
[
'id' => 22711,
'price' => 2910,
'size' => 12500,
],
[
'id' => 22712,
'price' => 3260,
'size' => 15000,
],
[
'id' => 22713,
'price' => 4130,
'size' => 20000,
],
[
'id' => 22714,
'price' => 3770,
'size' => 27000,
]
];

$required_size = 8; // Can change.
$container_install = 5;

foreach ( $available_containers as $v ){
foreach ( $available_containers as $v2 ){
foreach ($available_containers as $v3 ) {
foreach ( $available_containers as $v4 ){

$all_configs = [
[
'size' => $v['size'],
'configuration' => [ $v['size'] ],
'price' => $v['price'],
],
[
'size' => $v['size'] + $v2['size'],
'configuration' => [ $v['size'], $v2['size'] ],
'price' => $v['price'] + $v2['price'] + $container_install,
],
[
'size' => $v['size'] + $v2['size'] + $v3['size'],
'configuration' => [ $v['size'], $v2['size'], $v3['size'] ],
'price' => $v['price'] + $v2['price'] + $v3['price'] + $container_install + $container_install,
],
[
'size' => $v['size'] + $v2['size'] + $v3['size'] + $v4['size'],
'configuration' => [ $v['size'], $v2['size'], $v3['size'], $v4['size'] ],
'price' => $v['price'] + $v2['price'] + $v3['price'] + $v4['price'] + $container_install + $container_install + $container_install,
],
];


foreach ( $all_configs as $c ){
if ( $c['size'] >= $required_size ){
$configuration[] = array(
'configuration' => $c['configuration'],
'size' => $c['size'],
'price' => $c['price'],
);
}
}
}
}
}
}


// Sort by price.
$sorted_configs = array_sort($configuration, 'price', SORT_ASC); // This function simply sorts all permutations by price
```








share







New contributor




Mando is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$

















    0












    $begingroup$


    I have multiple products that all have sizes and I need to find the cheapest configuration that meets the minimum required size.



    For example, John needs a minimum of 10 litres of storage - it can be more, but not less.



    There are 2L, 3L, 5L, 8L and 10L options (but this can change).



    As an example, it might be cheaper to get:




    • 1x10L container OR

    • 2x5L containers OR

    • 1x2L, 1x3L and 1x5L OR

    • 4x3L (this one is over 10 L, but it is still possible that it will be cheaper)


    So far I've tried looping over and over up to 4 times (because typically the maximum requirement will be 40 L), but in some cases I am running out of memory, and it doesn't seem like the most efficient way of doing it.




    // Size is in mL

    $available_containers = [
    [
    'id' => 22700,
    'price' => 1190,
    'size' => 2000,
    ],
    [
    'id' => 22701,
    'price' => 1245,
    'size' => 3000,
    ],
    [
    'id' => 22702,
    'price' => 1415,
    'size' => 4000,
    ],
    [
    'id' => 22715,
    'price' => 12300,
    'size' => 5000,
    ],
    [
    'id' => 22706,
    'price' => 1740,
    'size' => 5000,
    ],
    [
    'id' => 22703,
    'price' => 1510,
    'size' => 5000,
    ],
    [
    'id' => 22707,
    'price' => 1790,
    'size' => 6000,
    ],
    [
    'id' => 22704,
    'price' => 1770,
    'size' => 6000,
    ],
    [
    'id' => 22708,
    'price' => 2215,
    'size' => 7000,
    ],
    [
    'id' => 22705,
    'price' => 2195,
    'size' => 8200,
    ],
    [
    'id' => 22709,
    'price' => 2660,
    'size' => 8200,
    ],
    [
    'id' => 22710,
    'price' => 2799,
    'size' => 10000,
    ],
    [
    'id' => 22711,
    'price' => 2910,
    'size' => 12500,
    ],
    [
    'id' => 22712,
    'price' => 3260,
    'size' => 15000,
    ],
    [
    'id' => 22713,
    'price' => 4130,
    'size' => 20000,
    ],
    [
    'id' => 22714,
    'price' => 3770,
    'size' => 27000,
    ]
    ];

    $required_size = 8; // Can change.
    $container_install = 5;

    foreach ( $available_containers as $v ){
    foreach ( $available_containers as $v2 ){
    foreach ($available_containers as $v3 ) {
    foreach ( $available_containers as $v4 ){

    $all_configs = [
    [
    'size' => $v['size'],
    'configuration' => [ $v['size'] ],
    'price' => $v['price'],
    ],
    [
    'size' => $v['size'] + $v2['size'],
    'configuration' => [ $v['size'], $v2['size'] ],
    'price' => $v['price'] + $v2['price'] + $container_install,
    ],
    [
    'size' => $v['size'] + $v2['size'] + $v3['size'],
    'configuration' => [ $v['size'], $v2['size'], $v3['size'] ],
    'price' => $v['price'] + $v2['price'] + $v3['price'] + $container_install + $container_install,
    ],
    [
    'size' => $v['size'] + $v2['size'] + $v3['size'] + $v4['size'],
    'configuration' => [ $v['size'], $v2['size'], $v3['size'], $v4['size'] ],
    'price' => $v['price'] + $v2['price'] + $v3['price'] + $v4['price'] + $container_install + $container_install + $container_install,
    ],
    ];


    foreach ( $all_configs as $c ){
    if ( $c['size'] >= $required_size ){
    $configuration[] = array(
    'configuration' => $c['configuration'],
    'size' => $c['size'],
    'price' => $c['price'],
    );
    }
    }
    }
    }
    }
    }


    // Sort by price.
    $sorted_configs = array_sort($configuration, 'price', SORT_ASC); // This function simply sorts all permutations by price
    ```








    share







    New contributor




    Mando is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      0












      0








      0





      $begingroup$


      I have multiple products that all have sizes and I need to find the cheapest configuration that meets the minimum required size.



      For example, John needs a minimum of 10 litres of storage - it can be more, but not less.



      There are 2L, 3L, 5L, 8L and 10L options (but this can change).



      As an example, it might be cheaper to get:




      • 1x10L container OR

      • 2x5L containers OR

      • 1x2L, 1x3L and 1x5L OR

      • 4x3L (this one is over 10 L, but it is still possible that it will be cheaper)


      So far I've tried looping over and over up to 4 times (because typically the maximum requirement will be 40 L), but in some cases I am running out of memory, and it doesn't seem like the most efficient way of doing it.




      // Size is in mL

      $available_containers = [
      [
      'id' => 22700,
      'price' => 1190,
      'size' => 2000,
      ],
      [
      'id' => 22701,
      'price' => 1245,
      'size' => 3000,
      ],
      [
      'id' => 22702,
      'price' => 1415,
      'size' => 4000,
      ],
      [
      'id' => 22715,
      'price' => 12300,
      'size' => 5000,
      ],
      [
      'id' => 22706,
      'price' => 1740,
      'size' => 5000,
      ],
      [
      'id' => 22703,
      'price' => 1510,
      'size' => 5000,
      ],
      [
      'id' => 22707,
      'price' => 1790,
      'size' => 6000,
      ],
      [
      'id' => 22704,
      'price' => 1770,
      'size' => 6000,
      ],
      [
      'id' => 22708,
      'price' => 2215,
      'size' => 7000,
      ],
      [
      'id' => 22705,
      'price' => 2195,
      'size' => 8200,
      ],
      [
      'id' => 22709,
      'price' => 2660,
      'size' => 8200,
      ],
      [
      'id' => 22710,
      'price' => 2799,
      'size' => 10000,
      ],
      [
      'id' => 22711,
      'price' => 2910,
      'size' => 12500,
      ],
      [
      'id' => 22712,
      'price' => 3260,
      'size' => 15000,
      ],
      [
      'id' => 22713,
      'price' => 4130,
      'size' => 20000,
      ],
      [
      'id' => 22714,
      'price' => 3770,
      'size' => 27000,
      ]
      ];

      $required_size = 8; // Can change.
      $container_install = 5;

      foreach ( $available_containers as $v ){
      foreach ( $available_containers as $v2 ){
      foreach ($available_containers as $v3 ) {
      foreach ( $available_containers as $v4 ){

      $all_configs = [
      [
      'size' => $v['size'],
      'configuration' => [ $v['size'] ],
      'price' => $v['price'],
      ],
      [
      'size' => $v['size'] + $v2['size'],
      'configuration' => [ $v['size'], $v2['size'] ],
      'price' => $v['price'] + $v2['price'] + $container_install,
      ],
      [
      'size' => $v['size'] + $v2['size'] + $v3['size'],
      'configuration' => [ $v['size'], $v2['size'], $v3['size'] ],
      'price' => $v['price'] + $v2['price'] + $v3['price'] + $container_install + $container_install,
      ],
      [
      'size' => $v['size'] + $v2['size'] + $v3['size'] + $v4['size'],
      'configuration' => [ $v['size'], $v2['size'], $v3['size'], $v4['size'] ],
      'price' => $v['price'] + $v2['price'] + $v3['price'] + $v4['price'] + $container_install + $container_install + $container_install,
      ],
      ];


      foreach ( $all_configs as $c ){
      if ( $c['size'] >= $required_size ){
      $configuration[] = array(
      'configuration' => $c['configuration'],
      'size' => $c['size'],
      'price' => $c['price'],
      );
      }
      }
      }
      }
      }
      }


      // Sort by price.
      $sorted_configs = array_sort($configuration, 'price', SORT_ASC); // This function simply sorts all permutations by price
      ```








      share







      New contributor




      Mando is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      I have multiple products that all have sizes and I need to find the cheapest configuration that meets the minimum required size.



      For example, John needs a minimum of 10 litres of storage - it can be more, but not less.



      There are 2L, 3L, 5L, 8L and 10L options (but this can change).



      As an example, it might be cheaper to get:




      • 1x10L container OR

      • 2x5L containers OR

      • 1x2L, 1x3L and 1x5L OR

      • 4x3L (this one is over 10 L, but it is still possible that it will be cheaper)


      So far I've tried looping over and over up to 4 times (because typically the maximum requirement will be 40 L), but in some cases I am running out of memory, and it doesn't seem like the most efficient way of doing it.




      // Size is in mL

      $available_containers = [
      [
      'id' => 22700,
      'price' => 1190,
      'size' => 2000,
      ],
      [
      'id' => 22701,
      'price' => 1245,
      'size' => 3000,
      ],
      [
      'id' => 22702,
      'price' => 1415,
      'size' => 4000,
      ],
      [
      'id' => 22715,
      'price' => 12300,
      'size' => 5000,
      ],
      [
      'id' => 22706,
      'price' => 1740,
      'size' => 5000,
      ],
      [
      'id' => 22703,
      'price' => 1510,
      'size' => 5000,
      ],
      [
      'id' => 22707,
      'price' => 1790,
      'size' => 6000,
      ],
      [
      'id' => 22704,
      'price' => 1770,
      'size' => 6000,
      ],
      [
      'id' => 22708,
      'price' => 2215,
      'size' => 7000,
      ],
      [
      'id' => 22705,
      'price' => 2195,
      'size' => 8200,
      ],
      [
      'id' => 22709,
      'price' => 2660,
      'size' => 8200,
      ],
      [
      'id' => 22710,
      'price' => 2799,
      'size' => 10000,
      ],
      [
      'id' => 22711,
      'price' => 2910,
      'size' => 12500,
      ],
      [
      'id' => 22712,
      'price' => 3260,
      'size' => 15000,
      ],
      [
      'id' => 22713,
      'price' => 4130,
      'size' => 20000,
      ],
      [
      'id' => 22714,
      'price' => 3770,
      'size' => 27000,
      ]
      ];

      $required_size = 8; // Can change.
      $container_install = 5;

      foreach ( $available_containers as $v ){
      foreach ( $available_containers as $v2 ){
      foreach ($available_containers as $v3 ) {
      foreach ( $available_containers as $v4 ){

      $all_configs = [
      [
      'size' => $v['size'],
      'configuration' => [ $v['size'] ],
      'price' => $v['price'],
      ],
      [
      'size' => $v['size'] + $v2['size'],
      'configuration' => [ $v['size'], $v2['size'] ],
      'price' => $v['price'] + $v2['price'] + $container_install,
      ],
      [
      'size' => $v['size'] + $v2['size'] + $v3['size'],
      'configuration' => [ $v['size'], $v2['size'], $v3['size'] ],
      'price' => $v['price'] + $v2['price'] + $v3['price'] + $container_install + $container_install,
      ],
      [
      'size' => $v['size'] + $v2['size'] + $v3['size'] + $v4['size'],
      'configuration' => [ $v['size'], $v2['size'], $v3['size'], $v4['size'] ],
      'price' => $v['price'] + $v2['price'] + $v3['price'] + $v4['price'] + $container_install + $container_install + $container_install,
      ],
      ];


      foreach ( $all_configs as $c ){
      if ( $c['size'] >= $required_size ){
      $configuration[] = array(
      'configuration' => $c['configuration'],
      'size' => $c['size'],
      'price' => $c['price'],
      );
      }
      }
      }
      }
      }
      }


      // Sort by price.
      $sorted_configs = array_sort($configuration, 'price', SORT_ASC); // This function simply sorts all permutations by price
      ```






      php mathematics





      share







      New contributor




      Mando is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      Mando is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      Mando is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 5 mins ago









      MandoMando

      11




      11




      New contributor




      Mando is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Mando is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Mando is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Mando is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214881%2fphp-find-cheapest-permutation%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Mando is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Mando is a new contributor. Be nice, and check out our Code of Conduct.













          Mando is a new contributor. Be nice, and check out our Code of Conduct.












          Mando is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214881%2fphp-find-cheapest-permutation%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Fairchild Swearingen Metro Inhaltsverzeichnis Geschichte | Innenausstattung | Nutzung | Zwischenfälle...

          Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...

          Marineschifffahrtleitung Inhaltsverzeichnis Geschichte | Heutige Organisation der NATO | Nationale und...