rockinaway Posted February 17, 2009 Share Posted February 17, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/145572-moving-through-an-array-in-a-foreach/ Share on other sites More sharing options...
premiso Posted February 17, 2009 Share Posted February 17, 2009 The Modulus Operator ( % ) if (($i % 2) == 0) { echo 'not odd'; }else { echo 'odd'; } Replace $i with what you want to test to be "odd" Quote Link to comment https://forums.phpfreaks.com/topic/145572-moving-through-an-array-in-a-foreach/#findComment-764234 Share on other sites More sharing options...
rockinaway Posted February 17, 2009 Author Share Posted February 17, 2009 That doesn't do anything to moving the array pointer to the correct value as I stated above. Quote Link to comment https://forums.phpfreaks.com/topic/145572-moving-through-an-array-in-a-foreach/#findComment-764404 Share on other sites More sharing options...
cooldude832 Posted February 17, 2009 Share Posted February 17, 2009 sounds like you should be using a database Quote Link to comment https://forums.phpfreaks.com/topic/145572-moving-through-an-array-in-a-foreach/#findComment-764409 Share on other sites More sharing options...
rockinaway Posted February 17, 2009 Author Share Posted February 17, 2009 ill eventually be using one, but I need to know how to do it in order to test it works. Quote Link to comment https://forums.phpfreaks.com/topic/145572-moving-through-an-array-in-a-foreach/#findComment-764418 Share on other sites More sharing options...
rockinaway Posted February 17, 2009 Author Share Posted February 17, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/145572-moving-through-an-array-in-a-foreach/#findComment-764556 Share on other sites More sharing options...
premiso Posted February 17, 2009 Share Posted February 17, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/145572-moving-through-an-array-in-a-foreach/#findComment-764576 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.