Automatically build as many nested loops as I need? [duplicate]











up vote
-1
down vote

favorite













This question already has an answer here:




  • Permutations - all possible sets of numbers

    11 answers




I would like to make a list of every possible combination of inserted numbers. I figured out this way:



     for($x=1;$x<=3;$x++){
for($y=1;$y<=3;$y++){
for($e=1;$e<=3;$e++){

echo $x.$y.$e."</br>" ;

}
}
}


But the problem with these nested loops is that I have to put manually as many loops as I have digits in a number. Is there a way to make it automatically/programmatically?










share|improve this question













marked as duplicate by phadaphunk, Nick, Barmar php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 9 at 21:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Is your input an array of numbers or is it a string of numbers?
    – GrumpyCrouton
    Nov 9 at 19:54










  • I imagined it as a string of numbers, but it is not necessary.
    – Bunny Davis
    Nov 9 at 19:58












  • Does stackoverflow.com/questions/5506888/… help
    – Nigel Ren
    Nov 9 at 20:31















up vote
-1
down vote

favorite













This question already has an answer here:




  • Permutations - all possible sets of numbers

    11 answers




I would like to make a list of every possible combination of inserted numbers. I figured out this way:



     for($x=1;$x<=3;$x++){
for($y=1;$y<=3;$y++){
for($e=1;$e<=3;$e++){

echo $x.$y.$e."</br>" ;

}
}
}


But the problem with these nested loops is that I have to put manually as many loops as I have digits in a number. Is there a way to make it automatically/programmatically?










share|improve this question













marked as duplicate by phadaphunk, Nick, Barmar php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 9 at 21:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Is your input an array of numbers or is it a string of numbers?
    – GrumpyCrouton
    Nov 9 at 19:54










  • I imagined it as a string of numbers, but it is not necessary.
    – Bunny Davis
    Nov 9 at 19:58












  • Does stackoverflow.com/questions/5506888/… help
    – Nigel Ren
    Nov 9 at 20:31













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite












This question already has an answer here:




  • Permutations - all possible sets of numbers

    11 answers




I would like to make a list of every possible combination of inserted numbers. I figured out this way:



     for($x=1;$x<=3;$x++){
for($y=1;$y<=3;$y++){
for($e=1;$e<=3;$e++){

echo $x.$y.$e."</br>" ;

}
}
}


But the problem with these nested loops is that I have to put manually as many loops as I have digits in a number. Is there a way to make it automatically/programmatically?










share|improve this question














This question already has an answer here:




  • Permutations - all possible sets of numbers

    11 answers




I would like to make a list of every possible combination of inserted numbers. I figured out this way:



     for($x=1;$x<=3;$x++){
for($y=1;$y<=3;$y++){
for($e=1;$e<=3;$e++){

echo $x.$y.$e."</br>" ;

}
}
}


But the problem with these nested loops is that I have to put manually as many loops as I have digits in a number. Is there a way to make it automatically/programmatically?





This question already has an answer here:




  • Permutations - all possible sets of numbers

    11 answers








php loops nested






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 19:46









Bunny Davis

113




113




marked as duplicate by phadaphunk, Nick, Barmar php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 9 at 21:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by phadaphunk, Nick, Barmar php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 9 at 21:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Is your input an array of numbers or is it a string of numbers?
    – GrumpyCrouton
    Nov 9 at 19:54










  • I imagined it as a string of numbers, but it is not necessary.
    – Bunny Davis
    Nov 9 at 19:58












  • Does stackoverflow.com/questions/5506888/… help
    – Nigel Ren
    Nov 9 at 20:31


















  • Is your input an array of numbers or is it a string of numbers?
    – GrumpyCrouton
    Nov 9 at 19:54










  • I imagined it as a string of numbers, but it is not necessary.
    – Bunny Davis
    Nov 9 at 19:58












  • Does stackoverflow.com/questions/5506888/… help
    – Nigel Ren
    Nov 9 at 20:31
















Is your input an array of numbers or is it a string of numbers?
– GrumpyCrouton
Nov 9 at 19:54




Is your input an array of numbers or is it a string of numbers?
– GrumpyCrouton
Nov 9 at 19:54












I imagined it as a string of numbers, but it is not necessary.
– Bunny Davis
Nov 9 at 19:58






I imagined it as a string of numbers, but it is not necessary.
– Bunny Davis
Nov 9 at 19:58














Does stackoverflow.com/questions/5506888/… help
– Nigel Ren
Nov 9 at 20:31




Does stackoverflow.com/questions/5506888/… help
– Nigel Ren
Nov 9 at 20:31












1 Answer
1






active

oldest

votes

















up vote
0
down vote













You can use a recursive function to loop again when necessary.



For example, this function prints out the numbers 1-3 for a given number of digits (minimum allowed value is 1, any lower it will still output all single-digit numbers in that range):



function variations($digits, $prefix = '') {
for ($i = 1; $i <= 3; $i++) {
$variation = $prefix . $i;

if ($digits > 1) {
variations($digits - 1, $variation);
} else {
echo $variation . "<br />";
}
}
}

variations(1);
/*
1<br />2<br />3<br />
*/

variations(2);
/*
11<br />12<br />13<br />21<br />22<br />23<br />31<br />32<br />33<br />
*/


https://3v4l.org/Rj2u7 (outputs a linebreak instead of "<br />" for display purposes)



Here's a slight variation that returns the generated numbers in an array instead of echo'ing them directly, so you can use them for some other purpose if you'd like:



function variations($digits, $prefix = '') {
$result = ;

for ($i = 1; $i <= 3; $i++) {
$variation = $prefix . $i;

if ($digits > 1) {
$result = array_merge($result, variations($digits - 1, $variation));
} else {
$result = $variation;
}
}

return $result;
}

print_r(variations(1));
/*
Array
(
[0] => 1
[1] => 2
[2] => 3
)
*/

print_r(variations(2));
/*
Array
(
[0] => 11
[1] => 12
[2] => 13
[3] => 21
[4] => 22
[5] => 23
[6] => 31
[7] => 32
[8] => 33
)
*/


https://3v4l.org/LBrjA






share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    You can use a recursive function to loop again when necessary.



    For example, this function prints out the numbers 1-3 for a given number of digits (minimum allowed value is 1, any lower it will still output all single-digit numbers in that range):



    function variations($digits, $prefix = '') {
    for ($i = 1; $i <= 3; $i++) {
    $variation = $prefix . $i;

    if ($digits > 1) {
    variations($digits - 1, $variation);
    } else {
    echo $variation . "<br />";
    }
    }
    }

    variations(1);
    /*
    1<br />2<br />3<br />
    */

    variations(2);
    /*
    11<br />12<br />13<br />21<br />22<br />23<br />31<br />32<br />33<br />
    */


    https://3v4l.org/Rj2u7 (outputs a linebreak instead of "<br />" for display purposes)



    Here's a slight variation that returns the generated numbers in an array instead of echo'ing them directly, so you can use them for some other purpose if you'd like:



    function variations($digits, $prefix = '') {
    $result = ;

    for ($i = 1; $i <= 3; $i++) {
    $variation = $prefix . $i;

    if ($digits > 1) {
    $result = array_merge($result, variations($digits - 1, $variation));
    } else {
    $result = $variation;
    }
    }

    return $result;
    }

    print_r(variations(1));
    /*
    Array
    (
    [0] => 1
    [1] => 2
    [2] => 3
    )
    */

    print_r(variations(2));
    /*
    Array
    (
    [0] => 11
    [1] => 12
    [2] => 13
    [3] => 21
    [4] => 22
    [5] => 23
    [6] => 31
    [7] => 32
    [8] => 33
    )
    */


    https://3v4l.org/LBrjA






    share|improve this answer

























      up vote
      0
      down vote













      You can use a recursive function to loop again when necessary.



      For example, this function prints out the numbers 1-3 for a given number of digits (minimum allowed value is 1, any lower it will still output all single-digit numbers in that range):



      function variations($digits, $prefix = '') {
      for ($i = 1; $i <= 3; $i++) {
      $variation = $prefix . $i;

      if ($digits > 1) {
      variations($digits - 1, $variation);
      } else {
      echo $variation . "<br />";
      }
      }
      }

      variations(1);
      /*
      1<br />2<br />3<br />
      */

      variations(2);
      /*
      11<br />12<br />13<br />21<br />22<br />23<br />31<br />32<br />33<br />
      */


      https://3v4l.org/Rj2u7 (outputs a linebreak instead of "<br />" for display purposes)



      Here's a slight variation that returns the generated numbers in an array instead of echo'ing them directly, so you can use them for some other purpose if you'd like:



      function variations($digits, $prefix = '') {
      $result = ;

      for ($i = 1; $i <= 3; $i++) {
      $variation = $prefix . $i;

      if ($digits > 1) {
      $result = array_merge($result, variations($digits - 1, $variation));
      } else {
      $result = $variation;
      }
      }

      return $result;
      }

      print_r(variations(1));
      /*
      Array
      (
      [0] => 1
      [1] => 2
      [2] => 3
      )
      */

      print_r(variations(2));
      /*
      Array
      (
      [0] => 11
      [1] => 12
      [2] => 13
      [3] => 21
      [4] => 22
      [5] => 23
      [6] => 31
      [7] => 32
      [8] => 33
      )
      */


      https://3v4l.org/LBrjA






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You can use a recursive function to loop again when necessary.



        For example, this function prints out the numbers 1-3 for a given number of digits (minimum allowed value is 1, any lower it will still output all single-digit numbers in that range):



        function variations($digits, $prefix = '') {
        for ($i = 1; $i <= 3; $i++) {
        $variation = $prefix . $i;

        if ($digits > 1) {
        variations($digits - 1, $variation);
        } else {
        echo $variation . "<br />";
        }
        }
        }

        variations(1);
        /*
        1<br />2<br />3<br />
        */

        variations(2);
        /*
        11<br />12<br />13<br />21<br />22<br />23<br />31<br />32<br />33<br />
        */


        https://3v4l.org/Rj2u7 (outputs a linebreak instead of "<br />" for display purposes)



        Here's a slight variation that returns the generated numbers in an array instead of echo'ing them directly, so you can use them for some other purpose if you'd like:



        function variations($digits, $prefix = '') {
        $result = ;

        for ($i = 1; $i <= 3; $i++) {
        $variation = $prefix . $i;

        if ($digits > 1) {
        $result = array_merge($result, variations($digits - 1, $variation));
        } else {
        $result = $variation;
        }
        }

        return $result;
        }

        print_r(variations(1));
        /*
        Array
        (
        [0] => 1
        [1] => 2
        [2] => 3
        )
        */

        print_r(variations(2));
        /*
        Array
        (
        [0] => 11
        [1] => 12
        [2] => 13
        [3] => 21
        [4] => 22
        [5] => 23
        [6] => 31
        [7] => 32
        [8] => 33
        )
        */


        https://3v4l.org/LBrjA






        share|improve this answer












        You can use a recursive function to loop again when necessary.



        For example, this function prints out the numbers 1-3 for a given number of digits (minimum allowed value is 1, any lower it will still output all single-digit numbers in that range):



        function variations($digits, $prefix = '') {
        for ($i = 1; $i <= 3; $i++) {
        $variation = $prefix . $i;

        if ($digits > 1) {
        variations($digits - 1, $variation);
        } else {
        echo $variation . "<br />";
        }
        }
        }

        variations(1);
        /*
        1<br />2<br />3<br />
        */

        variations(2);
        /*
        11<br />12<br />13<br />21<br />22<br />23<br />31<br />32<br />33<br />
        */


        https://3v4l.org/Rj2u7 (outputs a linebreak instead of "<br />" for display purposes)



        Here's a slight variation that returns the generated numbers in an array instead of echo'ing them directly, so you can use them for some other purpose if you'd like:



        function variations($digits, $prefix = '') {
        $result = ;

        for ($i = 1; $i <= 3; $i++) {
        $variation = $prefix . $i;

        if ($digits > 1) {
        $result = array_merge($result, variations($digits - 1, $variation));
        } else {
        $result = $variation;
        }
        }

        return $result;
        }

        print_r(variations(1));
        /*
        Array
        (
        [0] => 1
        [1] => 2
        [2] => 3
        )
        */

        print_r(variations(2));
        /*
        Array
        (
        [0] => 11
        [1] => 12
        [2] => 13
        [3] => 21
        [4] => 22
        [5] => 23
        [6] => 31
        [7] => 32
        [8] => 33
        )
        */


        https://3v4l.org/LBrjA







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 20:44









        rickdenhaan

        5,4251221




        5,4251221















            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()