Welcome Guest, Not a member yet? Register   Sign In
Issue with generating multi dimensional array.
#1

[eluser]kyleect[/eluser]
Ok, this is a little complicated to explain so I will put down my code first.

Here is what I have:

Code:
<?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";
}
?&gt;

This fills $hours with 34 entries, each with an array of 6 random numbers between 1-5. Ok, that works. Looks like this:

Quote: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

There is a problem though. On each of those lines, if each of those values are above 5, the value in the same place on the next line has to be 4 or below. I'm having issues with this. Here is what I've tried and what I've gotten:

Code:
&lt;?php
header('Content-Type: text/plain');

$hours = array();

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

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

Quote:<br />
<b>Notice</b>: Undefined offset: -1 in <b>/Users/k/htdocs/gen.php</b> on line <b>12</b><br />
<br />
<b>Notice</b>: Undefined offset: -1 in <b>/Users/k/htdocs/gen.php</b> on line <b>12</b><br />
<br />
<b>Notice</b>: Undefined offset: -1 in <b>/Users/k/htdocs/gen.php</b> on line <b>12</b><br />
<br />
<b>Notice</b>: Undefined offset: -1 in <b>/Users/k/htdocs/gen.php</b> on line <b>12</b><br />
<br />
<b>Notice</b>: Undefined offset: -1 in <b>/Users/k/htdocs/gen.php</b> on line <b>12</b><br />
<br />
<b>Notice</b>: Undefined offset: -1 in <b>/Users/k/htdocs/gen.php</b> on line <b>12</b><br />
0:0:1 0:0:3 0:0:9 0:0:1 0:0:4 0:0:8
0:0:1 0:0:2 0:0:5 0:0:2 0:0:1 0:0:3
0:0:5 0:0:4 0:0:5 0:0:3 0:0:3 0:0:4
0:0:5 0:0:5 0:0:1 0:0:5 0:0:4 0:0:5
0:0:3 0:0:5 0:0:5 0:0:3 0:0:4 0:0:1
0:0:4 0:0:2 0:0:3 0:0:5 0:0:1 0:0:3
0:0:3 0:0:5 0:0:2 0:0:3 0:0:4 0:0:2
0:0:2 0:0:3 0:0:5 0:0:1 0:0:1 0:0:5
0:0:5 0:0:4 0:0:2 0:0:1 0:0:4 0:0:4
0:0:5 0:0:4 0:0:5 0:0:3 0:0:5 0:0:2
0:0:5 0:0:4 0:0:5 0:0:3 0:0:5 0:0:2
0:0:5 0:0:5 0:0:2 0:0:2 0:0:1 0:0:5
0:0:5 0:0:1 0:0:5 0:0:5 0:0:1 0:0:3
0:0:4 0:0:4 0:0:3 0:0:4 0:0:2 0:0:2
0:0:4 0:0:4 0:0:3 0:0:4 0:0:2 0:0:3
0:0:1 0:0:4 0:0:2 0:0:2 0:0:4 0:0:1
0:0:3 0:0:1 0:0:3 0:0:5 0:0:3 0:0:1
0:0:5 0:0:3 0:0:4 0:0:2 0:0:4 0:0:5
0:0:2 0:0:4 0:0:3 0:0:5 0:0:4 0:0:3
0:0:4 0:0:3 0:0:2 0:0:5 0:0:5 0:0:2
0:0:3 0:0:1 0:0:1 0:0:1 0:0:4 0:0:3
0:0:2 0:0:5 0:0:2 0:0:5 0:0:2 0:0:1
0:0:3 0:0:4 0:0:4 0:0:3 0:0:4 0:0:1
0:0:1 0:0:4 0:0:3 0:0:1 0:0:4 0:0:4
0:0:1 0:0:4 0:0:2 0:0:2 0:0:4 0:0:1
0:0:4 0:0:2 0:0:3 0:0:1 0:0:5 0:0:5
0:0:5 0:0:3 0:0:2 0:0:4 0:0:5 0:0:2
0:0:4 0:0:5 0:0:5 0:0:2 0:0:4 0:0:5
0:0:5 0:0:3 0:0:1 0:0:2 0:0:3 0:0:3
0:0:1 0:0:4 0:0:5 0:0:4 0:0:2 0:0:3
0:0:5 0:0:4 0:0:1 0:0:1 0:0:3 0:0:5
0:0:1 0:0:4 0:0:5 0:0:2 0:0:1 0:0:4
0:0:5 0:0:5 0:0:4 0:0:5 0:0:3 0:0:2
0:0:2 0:0:2 0:0:5 0:0:1 0:0:5 0:0:1
0:0:2 0:0:5 0:0:4 0:0:5 0:0:5 0:0:3

I've also tried:

Code:
$previous = ($a - 1);

Same error. Does anyone know how to make this work? Thanks!
#2

[eluser]kyleect[/eluser]
Figured out that it will error as I would be subtracting 1 from the first entry in the array, obivously an error so changed my code a bit:

Code:
&lt;?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";
}
?&gt;

but it now is ONLY pulling from the 1,10 random, not the 1,5
#3

[eluser]kyleect[/eluser]
Sorry, just posting what I find. The issue right now is this line:

Code:
if($a < 0){

it's evaluating true but shouldn't. That's why it keeps using 1,10 and not 1,5. Does anyone know why it would be doing this?
#4

[eluser]kyleect[/eluser]
Figured out it that it can't be less than 0 so changed it to this:

Code:
if($a == 0){

Here is what I have now:

Code:
&lt;?php
header('Content-Type: text/plain');

$lines = array();

for ($a=0; $a < 35; $a++) {
    
        $lines[$a] = array();

        for ($b=0; $b < 6; $b++) {
            if($a == 0){
                $seconds = mt_rand(1, 10);
            }else{
                if($lines[($a - 1)] > 5){
                    $seconds = mt_rand(1, 4);
                }else{
                    $seconds = mt_rand(1, 10);
                }
            }

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

        echo (implode("\t", $lines[$a]))."\n";
}
?&gt;

The first line is random 1,10 but all the other lines after that are 1,5.

Quote:0:0:8 0:0:9 0:0:4 0:0:7 0:0:4 0:0:6 This is 1,10
0:0:3 0:0:3 0:0:1 0:0:1 0:0:1 0:0:2 The rest of these all the way down are 1,5
0:0:2 0:0:4 0:0:1 0:0:1 0:0:2 0:0:3
0:0:1 0:0:4 0:0:3 0:0:4 0:0:4 0:0:1
0:0:2 0:0:1 0:0:3 0:0:4 0:0:1 0:0:4
0:0:1 0:0:2 0:0:3 0:0:3 0:0:4 0:0:1
0:0:3 0:0:4 0:0:4 0:0:1 0:0:1 0:0:4
0:0:3 0:0:3 0:0:1 0:0:4 0:0:1 0:0:1
0:0:3 0:0:4 0:0:1 0:0:3 0:0:2 0:0:3
0:0:3 0:0:3 0:0:2 0:0:2 0:0:3 0:0:2
0:0:4 0:0:4 0:0:3 0:0:1 0:0:1 0:0:2
0:0:3 0:0:4 0:0:1 0:0:4 0:0:4 0:0:1
0:0:1 0:0:3 0:0:2 0:0:3 0:0:4 0:0:1
0:0:1 0:0:2 0:0:2 0:0:2 0:0:3 0:0:3
0:0:4 0:0:4 0:0:1 0:0:2 0:0:4 0:0:2
0:0:1 0:0:1 0:0:1 0:0:3 0:0:1 0:0:4
0:0:1 0:0:2 0:0:1 0:0:2 0:0:1 0:0:3
0:0:2 0:0:3 0:0:1 0:0:2 0:0:4 0:0:2
0:0:4 0:0:3 0:0:1 0:0:3 0:0:3 0:0:2
0:0:3 0:0:1 0:0:4 0:0:4 0:0:4 0:0:1
0:0:1 0:0:1 0:0:3 0:0:2 0:0:2 0:0:4
0:0:2 0:0:1 0:0:1 0:0:3 0:0:4 0:0:4
0:0:1 0:0:3 0:0:1 0:0:1 0:0:2 0:0:2
0:0:1 0:0:4 0:0:3 0:0:1 0:0:2 0:0:3
0:0:1 0:0:3 0:0:3 0:0:1 0:0:3 0:0:3
0:0:2 0:0:3 0:0:4 0:0:4 0:0:4 0:0:3
0:0:2 0:0:2 0:0:4 0:0:1 0:0:1 0:0:3
0:0:1 0:0:1 0:0:3 0:0:3 0:0:2 0:0:1
0:0:2 0:0:4 0:0:1 0:0:3 0:0:2 0:0:2
0:0:4 0:0:3 0:0:1 0:0:4 0:0:3 0:0:2
0:0:3 0:0:4 0:0:4 0:0:4 0:0:4 0:0:2
0:0:2 0:0:3 0:0:1 0:0:4 0:0:3 0:0:1
0:0:1 0:0:1 0:0:4 0:0:4 0:0:1 0:0:2
0:0:4 0:0:2 0:0:4 0:0:2 0:0:4 0:0:4
0:0:4 0:0:4 0:0:1 0:0:2 0:0:3 0:0:2

Maybe I'm not explaining this correctly.

if $line[0][1] is more than 5 then $line[1][1] has to be less than 5. or if $line[23][3] is 9 then $line[24][3] has to be less than 5. Can anyone please help me with this?




Theme © iAndrew 2016 - Forum software by © MyBB