Jump to content

New to coding in general, Array question


Hasheem

Recommended Posts

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!
Link to comment
Share on other sites

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 by mac_gyver
code tags please
Link to comment
Share on other sites

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.

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.