1

I have an array like so:

Array ( [0] => 0 [1] => 0 [2] => 0 );

there are 3 elements (3 integers) and I want them to increment from 0 to 36;

I understand that the best way for this is recursion because each element has to be checked to see if it is at the maximum ( 36 ) and if it is, it sets the last element to 0 and increments the previous.

so my array basically wants to go like:

Array ( [0] => 0 [1] => 0 [2] => 0 );
Array ( [0] => 0 [1] => 0 [2] => 1 );
Array ( [0] => 0 [1] => 0 [2] => 2 );
...
Array ( [0] => 0 [1] => 0 [2] => 36 );
Array ( [0] => 0 [1] => 1 [2] => 0 );
Array ( [0] => 0 [1] => 1 [2] => 1 );
....
Array ( [0] => 0 [1] => 1 [2] => 36 );
Array ( [0] => 0 [1] => 2 [2] => 0 );

ETC ETC ETC

But I've got no idea how to do this recursively!

However the solution also needs to work for 4 elements and 5 elements and 6 etc etc!

Can anybody give me some direction?

1
  • 2
    is this supposed to represent a base-37 number? Commented Feb 10, 2012 at 12:53

6 Answers 6

2

Similar to Timurs answer, however slightly more efficient and takes a variable base.

$array = array(0, 0, 0);

function bloop(&$array, $amount, $base = 37)
{
    $i = count($array) - 1;
    while ($i >= 0) {
        $array[$i] = $amount % $base;
        $amount = ($amount - $array[$i--]) / $base;
    }
}

bloop($array, (37 * 37 * 2) + (37 * 5) + 8); // 2, 5, 8

var_dump($array);
Sign up to request clarification or add additional context in comments.

Comments

2

If you just want a base-37 number, consider using base_convert instead.

2 Comments

base convert has a max of 36??
good point. That seems like a pretty big coincidence...is this homework?
2
$limit = 36;
$step  = 1;
$array = array ( 0 , 0 , 0 );


function increment( array $array , $limit , $step )   {
    $result = $array = array_values( $array );
    while( count( array_keys( $result , $limit ) ) != count( $array ) ) {
        for( $i = 1 ; $i <= count( $result ) ; $i++ ) {
             while( $result[ count( $result )-$i ] < $limit )  {
                 $result[ count( $result )-$i ] += $step;
             }
        }
    }
    return $result;
}

var_dump( increment( $array , $limit , $step ) );

Comments

1
function fill($limit){
   $ret = array();
   while($i<=$limit){
      while($j<=$limit){
         while($k<=$limit){
            $ret[] = array($i,$j,$k);
            print_r($a);
            $k++;
         }
         $j++;
      }
      $i++;
   }
   return $ret;
}

fill(36);

Comments

1
function increment(&$array,$num){
    $plus = $num;
    for( $i=count($array)-1;$i>=0;$i-- ){
        $array[$i] += $plus;
        if( $array[$i]>36 ){
            $tmp = $array[$i]%37;
            $plus = ($array[$i]-$tmp)/37;
            $array[$i] = $tmp;
        }else{
            break;
        }
    }
}

// init array
$array = array( 0,0,0 );
// increment 100 times
increment($array,100);

var_dump($array);

2 Comments

this is PERFECT! However it goes from (0, 0, 0) - (0, 0, 36) correctly but after 0, 0, 36 it goes to 0, 1, 1 RATHER THAN 0, 1, 0... how could i modify this?
Oh... Wrong base for %... Edited my post
1

what about this??

<?php
$arr = array(0=>0,2=>0);
foreach (range(0,36) as $f )
{
   echo "<pre>";print_r(array_pad(array($f),3,0));
   echo "<pre>";print_r(array_pad(array($f),-3,0)); 
   $arr_n = $arr+array(1=>$f);
   ksort($arr_n);
   echo "<pre>"; print_r($arr_n);
}

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.