Jump to content

Recommended Posts

What I want to do is quite complicated, I beleive. I have and array, and a foreach loop for that array.

 


$array_batsmen = array (
'1' => array (
			'name'	=> 'John',
			'in'	=>	'0',
			'out'	=>	'0'
			),
'2'	=>	array (
			'name'	=> 'Guy',
			'in'	=>	'0',
			'out'	=>	'0'
			),
'3'	=>	array (
			'name'	=> 'Esset',
			'in'	=>	'0',
			'out'	=>	'0'
			));


foreach ($array_batsmen as $name_bat => $batsman)
{

				$batsman['in'] = 1;

				$number = rand(0, 190);
				// Ability
				if ($batsman['ability'] > 90) $number += 30;
				else if ($batsman['ability'] > 70) $number += 15;
				else if ($batsman['ability'] > 50) $number += 2;
				else $number = $number;

				// Ability
				if ($bowler['ability'] > 90) $number -= 30;
				else if ($bowler['ability'] > 70) $number -= 15;
				else if ($bowler['ability'] > 50) $number -= 2;
				else $number = $number;

				if ($number > 190) $run = 6;
				else if ($number > 160) $run = 4;
				else if ($number > 150)
				{


					$run = 3;
				}
				else if ($number > 80) $run = 2;
				else if ($number > 50) $run = 1;
				else if ($number > 10) $run = 0;



}

 

What I want to do is:

 

Whenever $run is an ODD number, I want to go through the array to find the person who is In BUT not out, and set the array on them, and so it would keep switching on odd numbers.  How would I do this?

Link to comment
https://forums.phpfreaks.com/topic/145572-moving-through-an-array-in-a-foreach/
Share on other sites

Any help? This is quite urgent.

 

I have used this:

 


else if ($number > 150)
				{
					foreach ($array_batsmen as $key => $value)
					{
						if ($key['in'] == 1 && $key['out'] == 0) [.........];
						echo 'done';
					}

					$run = 3;
				}

 

[.....] is what needs to be changed. I need to somehow restart the BIG foreach loop that wraps the whole thing. Any way of doing this?

I am not sure exactly what you want.

 

http://us.php.net/array

 

Look up the array functions. You will notice

 

reset — Set the internal pointer of an array to its first element

 

I would not use foreach for what you want, I would do a while or a for loop honestly and use each with reset etc. Really if that is not what you want, be a bit more descriptive, as

set the array on them
I have no clue what that means, and apparently not many other people do either.

 

Restate your questions with an example of how you want the end result to be, and try to be as descriptive as possible. Who knows, doing that may help you solve your own problem.

 

Either way, I would take a look at the different array functions that are available to you and see if those will help you out at all.

 

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.