Hasheem Posted June 3, 2014 Share Posted June 3, 2014 Sorry for the non code lingo, but I am wondering if I can pull a word from an array and use it to get a value from another array. I am making a simple dodgeball game engine. I want the coaches strategy to pick a certain attribute to base who to throw to. Here is the team array: //silverbacks 2=>array( 'team_id'=>2, 'name'=>'Silverbacks', 'script'=>'<img src="Team Names Small-14.png" alt="Silverbacks" height="25" width="100">', 'primary color'=> '#4E5A5C', 'secondary color'=> '#C5CDCF', 'coach'=>array( 'name'=>'Baldwin', 'strategies'=>array( 'pick_thrower'=>'worst', 'attribute'=>'rating', <--This is the attribute the coach will use to pick the best player 'pick_dodger'=>'worst', 'dattribute'=>'dodging', ), ), 'players'=>array( 7=>array( 'player_id'=>7, 'name'=>'Carter', 'catching'=>34, 'throwing'=>49, 'dodging'=>21, 'rating'=>65, <-- This is the players rating, is there way to pull this value by having the same word 'rating' in each array, or will I have to do it another way? 'loyalty'=>32, 'speed'=>4.1, 'school'=>'Wheatfield', 'position'=>'C', 'number'=>0, ), 6=>array( 'player_id'=>6, 'name'=>'Arthur', 'catching'=>32, 'throwing'=>35, 'dodging'=>57, 'rating'=>23, 'loyalty'=>92, 'speed'=>4.8, 'school'=>'U. Valley', 'position'=>'R', 'number'=>14, ), 8=>array( 'player_id'=>8, 'name'=>'Kenneth', 'catching'=>34, 'throwing'=>89, 'dodging'=>36, 'rating'=>56, 'loyalty'=>88, 'speed'=>4.8, 'school'=>'U. Valley', 'position'=>'L', 'number'=>2, ), 9=>array( 'player_id'=>9, 'name'=>'S. Salazar', 'catching'=>66, 'throwing'=>54, 'dodging'=>53, 'rating'=>30, 'loyalty'=>76, 'speed'=>5.3, 'school'=>'Sanchito', 'position'=>'R', 'number'=>11, ), 10=>array( 'player_id'=>10, 'name'=>'Sanchito', 'catching'=>30, 'throwing'=>91, 'dodging'=>98, 'rating'=>65, 'loyalty'=>45, 'speed'=>5.5, 'school'=>'Puerto Rico', 'position'=>'L', 'number'=>16, ), ), ) ); Thanks alot! Quote Link to comment https://forums.phpfreaks.com/topic/288951-new-to-coding-in-general-array-question/ Share on other sites More sharing options...
Hasheem Posted June 3, 2014 Author Share Posted June 3, 2014 (edited) Here is my attempt, function get_thrower_strategy($strategy, $players) { $attribute = $coach['strategies']['attribute'][0]; if($strategy == 'best'): $best_throw_rating = 0; foreach($players as $player): if($player[$attribute] >= $best_throw_rating): $throwing_player = $player; $best_throw_rating = $player[$attribute]; endif; endforeach; elseif($strategy == 'worst'): $worst_throw_rating = 100; foreach($players as $player): if($player[$attribute] < $worst_throw_rating): $throwing_player = $player; $worst_throw_rating = $player[$attribute]; endif; endforeach; else: endif; return $player; } Edited June 3, 2014 by mac_gyver code tags please Quote Link to comment https://forums.phpfreaks.com/topic/288951-new-to-coding-in-general-array-question/#findComment-1481717 Share on other sites More sharing options...
mac_gyver Posted June 3, 2014 Share Posted June 3, 2014 Please use the forum's bbcode tags (the edit form's <> button) around code when posting it in the forum. i edited your post above. this makes it easier to read code. it would help if you reposted your sample data, but use var_export() on it so that someone could copy it as runnable php code. Quote Link to comment https://forums.phpfreaks.com/topic/288951-new-to-coding-in-general-array-question/#findComment-1481719 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.