Jump to content

Use current() in array in array


rockinaway

Recommended Posts

How do I use current for an array in an array?

 

Like:

 

$bowler_array = array(
			'rockinaway' => array(
							'overs' => '0',
							'runs' => '0',
							'wickets' => '0',
							),
			'test1' => array(
							'overs' => '0',
							'runs' => '0',
							'wickets' => '0',
							),
			'test2' => array(
							'overs' => '0',
							'runs' => '0',
							'wickets' => '0',
							));

Link to comment
Share on other sites

$bowler_array = array(
			'rockinaway' => array(
							'overs' => '0',
							'runs' => '0',
							'wickets' => '0',
							),
			'test1' => array(
							'overs' => '0',
							'runs' => '0',
							'wickets' => '0',
							),
			'test2' => array(
							'overs' => '0',
							'runs' => '0',
							'wickets' => '0',
							));

$overs = 0;

if ($overs < 50)
{
foreach($bowler_array as $key)
{
for ($i = 0; $i < 6; $i++)
{
	$runs = rand(-1,6);
	if ($runs == -1)
	{
		$bowler_array[$key]['wickets'] = current($bowler_array[$key]['wickets']) + 1;
	} else{

	$bowler_array[$key]['runs'] = current($bowler_array[$key]['runs']) + $runs;
	}

	if ($i == 6)
	{
		$bowler_array[$key]['overs'] = current($bowler_array[$key]['overs']) + 1;
	} else $bowler_array[$key]['overs'] = current($bowler_array[$key]['overs']) + 0.1;



}
}

print_r($bowler_array);
}

 

That is my code, very rough at the moment.

 

I have an array of the bowlers, then each bowler has another array of their stats.

 

I am having the overs done by a for and then adding it on, I need to use the current bowler from the array then update the array values inside the bowler array for the separate bowlers.. make sense?

Link to comment
Share on other sites

make sense?

Not really, I've read this 10 times and cannot figure out what you want or want to do.

 

FYI -

 

current($bowler_array[$key]['runs'] and current($bowler_array[$key]['wickets'] are both illegal code because the values in that data is not an array. current() needs an array, not a number passed into it.

 

current($bowler_array[$key]) would be valid code.

Link to comment
Share on other sites

Oh I get it.... :D

 

Right I updated it..

 

$bowler_array = array(
                                'rockinaway' => array(
                                                                'overs' => '0',
                                                                'runs' => '0',
                                                                'wickets' => '0',
                                                                ),
                                'test1' => array(
                                                                'overs' => '0',
                                                                'runs' => '0',
                                                                'wickets' => '0',
                                                                ),
                                'test2' => array(
                                                                'overs' => '0',
                                                                'runs' => '0',
                                                                'wickets' => '0',
                                                                ));
                                                                
$overs = 0;

if ($overs < 50)
{
foreach($bowler_array as $key)
{
        reset($bowler_array);
        for ($i = 0; $i < 6; $i++)
        {
                $runs = rand(-1,6);
                if ($runs == -1)
                {
                        $bowler_array[$key]['wickets'] = $bowler_array[$key]['wickets'] + 1;
                } else{
                
                $bowler_array[$key]['runs'] = $bowler_array[$key]['runs'] + $runs;
                }
                
                if ($i == 6)
                {
                        $bowler_array[$key]['overs'] = $bowler_array[$key]['overs'] + 1;
                } else $bowler_array[$key]['overs'] = $bowler_array[$key]['overs'] + 0.1;
                
                
                
        }
        
        next($bowler_array);
        }
        
        print_r($bowler_array);
}

 

But there still is the problem with using the key as I need to refer to the name of the bowlers generally, but I can't use them like $value in a foreach because it gives illegal offset, so what should I do..

Link to comment
Share on other sites

$bowler_array = array(
                                'rockinaway' => array(
                                                                'overs' => '0',
                                                                'runs' => '0',
                                                                'wickets' => '0',
                                                                ),
                                'test1' => array(
                                                                'overs' => '0',
                                                                'runs' => '0',
                                                                'wickets' => '0',
                                                                ),
                                'test2' => array(
                                                                'overs' => '0',
                                                                'runs' => '0',
                                                                'wickets' => '0',
                                                                ));
                                                                
$overs = 0;

if ($overs < 50)
{
foreach($bowler_array as $key => $value)
{

        for ($i = 0; $i < 7; $i++)
        {
                $runs = rand(-1,6);
                if ($runs == -1)
                {
                        $value['wickets'] = $value['wickets'] + 1;
                } else{
                
                $value['runs'] = $value['runs'] + $runs;
                }
                
                if ($i == 6)
                {
                        $value['overs'] = $value['overs'] + 1;
                } else $value['overs'] = $value['overs'] + 0.1;
                
                
                
        }
        }
        
        print_r($bowler_array);
}
                      
                
           

 

Got it without any errors, but it doesn't work. Sorted out the other stuff you mentioned, but it doesn't work, doesn't update the array values :S

Link to comment
Share on other sites

OK.. I try explaining as best as I can.

 

Right there is an array of bowlers who each have another array containing their stats. Then I use a foreach to update the stats of the bowlers.

 

for ($i = 0; $i < 7; $i++)
        {
                $runs = rand(-1,6);
                if ($runs == -1)
                {
                        $value['wickets'] = $value['wickets'] + 1;
                } else{
                
                $value['runs'] = $value['runs'] + $runs;
                }
                
                if ($i == 6)
                {
                        $value['overs'] = $value['overs'] + 1;
                } else $value['overs'] = $value['overs'] + 0.1;               
                


}

 

This controls the overs, so 6 balls. The $runs selects a random number and if the nuber in a minus then it is a wicket, else the runs are added to the run total of the bowler. Then if $i is == to 6 then it adds 1 over to the bowler, else it adds 0.1.

 

That is it really.

Link to comment
Share on other sites

Oh my. So this is cricket. I guess my full lack of knowledge about anything regarding cricket doe not help. Plus you assuming I know something about cricket does not help either. I'll just assume you know why you want random numbers, because I can't even begin to guess why.

 

Maybe try this out. I see why you might have had problems solving this without a good understanding of foreach. No promises on syntax, sorry if I missed a semi colon :)

while (list($key, $bowler) = each($bowler_array)) { // loop through each bowler
for ($i = 0; $i < 6; $i++) { // i have no idea why we are looping 6 times inside of each bowler
	$runs = rand(-1,6);
	if ($runs == -1)
		$bowler_array[$key]['wickets']++; // add one to the wicket
	else
		$bowler_array[$key]['runs'] += $runs; // runs added to run total?

	if ($i == 6)
		$bowler_array[$key]['overs'] += 1; // add one to overs
	else
		$bowler_array[$key]['overs'] += 0.1; // add .1 to overs

}
}

Link to comment
Share on other sites

As already pointed out, if you have for ($i = 0; $i < 6; $i++), the $i never has the value 6 within the loop.

 

try

if ($overs < 50)  // not sure where $overs is incremented elsewhere in your code
{
    foreach($bowler_array as $bowler => $stats) {
        for ($i=1; $i<=6; $i++ ) {
            $runs = rand(-1, 6);
            if ($runs==-1) {
                $stats['wickets']++;
            }
            else {
                $stats['runs'] += $runs;
            }
        }
        $stats['overs']++;
        $bowler_array[$bowler] = $stats;
        if (++$overs==50) break;  // exit the loop after 50th over
    }
    
}

echo '<pre>', print_r($bowler_array, true), '</pre>';

 

 

Link to comment
Share on other sites

Personal preference I guess, I prefer foreach() to list()=each().

 

(off topic)

In answer to your question "i have no idea why we are looping 6 times inside of each bowler",

 

a  bowler bowls an "over" of 6 balls. From each ball the batsman can score from 0 to 6 runs by running between the wickets that many times (the wickets are 22 yards, or 66ft or 1 chain apart), or he could be "out" (caught, bowled, leg before wicket, stumped or runout), hence the -1 option, the bowler gets a "wicket" and the next batsman comes out to the field and is in.

 

After the bowler has bowled his over, they change ends and the bowler from the other end of the wicket bowls an over at the other batsman (although if an odd of runs was scored off the last ball, it could at the same batsman).

 

It does seem strange, therefore, that we have here an array of three bowlers, when bowlers, like batsmen, are in pairs, one at each end of the wicket.

 

This goes on for five days a week over a five to ten week period, hence the obvious appeal.

 

I'm sure that clears it all up for you.

Link to comment
Share on other sites

As already pointed out, if you have for ($i = 0; $i < 6; $i++), the $i never has the value 6 within the loop.

 

try

if ($overs < 50)  // not sure where $overs is incremented elsewhere in your code
{
    foreach($bowler_array as $bowler => $stats) {
        for ($i=1; $i<=6; $i++ ) {
            $runs = rand(-1, 6);
            if ($runs==-1) {
                $stats['wickets']++;
            }
            else {
                $stats['runs'] += $runs;
            }
        }
        $stats['overs']++;
        $bowler_array[$bowler] = $stats;
        if (++$overs==50) break;  // exit the loop after 50th over
    }
    
}

echo '<pre>', print_r($bowler_array, true), '</pre>';

 

 

 

That is suitable I think, may need to tweak it for my needs slightly. WIll try it when I get home.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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