Jump to content

[SOLVED] Dysfunctional foreach statement..


Perad

Recommended Posts

OK, this is strange.

 

The count statement prints '3'. This is correct as there are 3 items in this array.

 

However the 'print $technology' statement only brings back one result. Can someone tell me why this is?

 

print count($defense);
foreach($defense as $technology=>$values) {
				print $technology;
				// Grab Unit Name
				$tech_d_name[] = $technology;
				$tech_d_form_name[] = str_replace(" ", "!", $technology);
				foreach ($defense as $value=>$attributes) {
					// Grab Unit costs
					if ($value == "price") {
						$tech_d_credits[] = $attributes;
					}
					if ($value == "description") {
						$tech_d_description[] = $attributes;
					}
					foreach ($attributes as $key=>$val) {
						switch ($key) {
							case "levels":
								$tech_d_levels[] = $val;
							break;
							case "scale":
								$tech_d_scale[] = $val;
							break;
						}							
					}
					if ($value == "resources") {
						foreach ($attributes as $key => $val) {
							switch ($key) {
								case "ore":
									$tech_d_cost_ore[] = $val;
								break;
								case "man":
									$tech_d_cost_man[] = $val;
								break;
								case "titanium":
									$tech_d_cost_titanium[] = $val;
								break;
								case "plasma":
									$tech_d_cost_plasma[] = $val;
								break;
								case "lead":
									$tech_d_cost_lead[] = $val;
								break;
							}
						}
					}
				}

Link to comment
Share on other sites

This is a multi-dimensional array so I feel that I have labeled it correctly.

 

technology name => technology values => value attributes

 

Anyway print_r outputs the following. I have added line breaks after each value of the $defense array.

 

Array (

 

[d_moat] => Array ( [price] => 5000 [description] => its a moat [resources] => Array ( [ore] => 100 [man] => 50 [titanium] => 0 [plasma] => 50 [lead] => 0 ) [bonus] => A rray ( [damage] => 0 [aoe] => 0 [enemy_att] => -5 [friendly_att] => 0 [friendly_def] => 0 [enemy_def] => -10 [affects_inf] => y [affects_arm] => y [affects_air] => n [levels] => 2 [scale] => 100 ) )

 

[d_mine field] => Array ( [price] => 5000 [description] => its a moat [resources] => Array ( [ore] => 100 [man] => 50 [titanium] => 0 [plasma] => 50 [lead] => 0 ) [bonus] => Array ( [damage] => 100 [aoe] => 2 [enemy_att] => 0 [friendly_att] => 0 [friendly_def] => 0 [enemy_def] => 0 [affects_inf] => y [affects_arm] => y [affects_air] => n [levels] => 1 [scale] => 0 ) )

 

[d_anti-air] => Array ( [price] => 10000 [description] => its a moat [resources] => Array ( [ore] => 100 [man] => 50 [titanium] => 0 [plasma] => 50 [lead] => 0 ) [bonus] => Array ( [damage] => 60 [aoe] => 1 [enemy_att] => 0 [friendly_att] => 0 [friendly_def] => 0 [enemy_def] => 0 [affects_inf] => n [affects_arm] => n [affects_air] => y [levels] => 5 [scale] => 110 ) ) )

 

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.