Jump to content

Issue with generating multi dimensional array.


maexus

Recommended Posts

Ok, this is a little complicated to explain so I will put down my code first.

 

Here is what I have:

 

<?php
header('Content-Type: text/plain');

$hours = array();

for ($a=0; $a < 35; $a++) { 
        $hours[$a] = array();
        
        for ($b=0; $b < 6; $b++) {
            $seconds = mt_rand(1, 5);
            $hours[$a][$b] = '0:0:'.$seconds; 
        }

        echo (implode("\t", $hours[$a]))."\n";
}
?>

 

and this is what it generates:

 

0:0:3   0:0:2   0:0:1   0:0:3   0:0:3   0:0:3
0:0:5   0:0:4   0:0:5   0:0:3   0:0:2   0:0:5
0:0:1   0:0:2   0:0:3   0:0:3   0:0:4   0:0:1
0:0:4   0:0:1   0:0:5   0:0:5   0:0:3   0:0:4
0:0:1   0:0:1   0:0:2   0:0:1   0:0:4   0:0:5
0:0:4   0:0:4   0:0:2   0:0:4   0:0:3   0:0:3
0:0:2   0:0:4   0:0:3   0:0:1   0:0:2   0:0:4
0:0:1   0:0:1   0:0:2   0:0:5   0:0:2   0:0:1
0:0:1   0:0:3   0:0:5   0:0:4   0:0:1   0:0:2
0:0:3   0:0:4   0:0:3   0:0:1   0:0:4   0:0:1
0:0:4   0:0:3   0:0:4   0:0:2   0:0:1   0:0:5
0:0:2   0:0:2   0:0:1   0:0:1   0:0:3   0:0:4
0:0:1   0:0:2   0:0:5   0:0:2   0:0:5   0:0:1
0:0:4   0:0:3   0:0:5   0:0:5   0:0:4   0:0:2
0:0:5   0:0:3   0:0:2   0:0:1   0:0:4   0:0:1
0:0:4   0:0:4   0:0:3   0:0:1   0:0:5   0:0:1
0:0:2   0:0:1   0:0:1   0:0:1   0:0:2   0:0:2
0:0:4   0:0:3   0:0:4   0:0:3   0:0:1   0:0:3
0:0:2   0:0:5   0:0:2   0:0:2   0:0:5   0:0:5
0:0:5   0:0:3   0:0:2   0:0:1   0:0:4   0:0:5
0:0:5   0:0:4   0:0:5   0:0:2   0:0:1   0:0:4
0:0:2   0:0:5   0:0:1   0:0:1   0:0:2   0:0:5
0:0:5   0:0:3   0:0:5   0:0:3   0:0:4   0:0:3
0:0:2   0:0:2   0:0:1   0:0:3   0:0:4   0:0:1
0:0:2   0:0:3   0:0:2   0:0:4   0:0:5   0:0:5
0:0:2   0:0:4   0:0:5   0:0:3   0:0:1   0:0:2
0:0:2   0:0:3   0:0:5   0:0:2   0:0:5   0:0:4
0:0:1   0:0:5   0:0:2   0:0:2   0:0:2   0:0:1
0:0:2   0:0:1   0:0:1   0:0:4   0:0:4   0:0:2
0:0:4   0:0:1   0:0:2   0:0:3   0:0:2   0:0:5
0:0:1   0:0:3   0:0:5   0:0:1   0:0:1   0:0:5
0:0:4   0:0:4   0:0:1   0:0:4   0:0:2   0:0:1
0:0:2   0:0:4   0:0:5   0:0:5   0:0:4   0:0:2
0:0:4   0:0:4   0:0:5   0:0:1   0:0:2   0:0:4
0:0:5   0:0:1   0:0:4   0:0:5   0:0:1   0:0:5

 

Here is what I've tried but it only pulls from the random(1,10) not choosing between the 1,10 and 1,5:

 

<?php
header('Content-Type: text/plain');

$hours = array();

for ($a=0; $a < 35; $a++) { 
        $hours[$a] = array();
        
        for ($b=0; $b < 6; $b++) {

            if($a < 0){
                $previous = $a - 1;

                if($hours[$previous] > 5){
                    $seconds = mt_rand(1, 5);
                }else{
                    $seconds = mt_rand(1, 10);
                }
            }else{
                $seconds = mt_rand(1, 10);
            }

            
            
            
            $hours[$a][$b] = '0:0:'.$seconds; 
        }

        echo (implode("\t", $hours[$a]))."\n";
}
?>

 

Can anyone help? If you need more information, please check http://codeigniter.com/forums/viewthread/103084/

I'm not following what you are trying to do, but I think I see the problem.

 

In the last code you posted:

 1 <?php
2 header('Content-Type: text/plain');
3
4 $hours = array();
5
6 for ($a=0; $a < 35; $a++) { 
7         $hours[$a] = array();
8        
9         for ($b=0; $b < 6; $b++) {
10
11             if($a < 0){
12                 $previous = $a - 1;
13 
14                 if($hours[$previous] > 5){
15                     $seconds = mt_rand(1, 5);
16                 }else{
17                     $seconds = mt_rand(1, 10);
18                 }
19             }else{
20                 $seconds = mt_rand(1, 10);
21             }
22            $hours[$a][$b] = '0:0:'.$seconds; 
23        }
24
25         echo (implode("\t", $hours[$a]))."\n";
26 }
27 ?>

 

Line 6 sets the value of $a from 0 to 34. On line 11 you have a test to see if ($a <0). That condition will never be true, so the else statement (Line 20) is always run.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.