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
https://forums.phpfreaks.com/topic/81328-solved-dysfunctional-foreach-statement/
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 ) ) )

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.