Perad Posted December 12, 2007 Share Posted December 12, 2007 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; } } } } Quote Link to comment Share on other sites More sharing options...
Yesideez Posted December 12, 2007 Share Posted December 12, 2007 Shouldn't you be using "print $defense"? Not sure myself as I'm not too familiar using keys with foreach. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 12, 2007 Share Posted December 12, 2007 use print_r($defense) and see what it outputs Quote Link to comment Share on other sites More sharing options...
papaface Posted December 12, 2007 Share Posted December 12, 2007 Just a tip $defense as $value=>$attributes Name the variables correctly. You have assigned $value, but it actually is the key of the array element. Should be: $defense as $key=>$value Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 12, 2007 Share Posted December 12, 2007 the problem is your using $defense in a nested foreach loop I think that would screw up the internal counter Quote Link to comment Share on other sites More sharing options...
Perad Posted December 12, 2007 Author Share Posted December 12, 2007 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 ) ) ) Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 12, 2007 Share Posted December 12, 2007 this foreach ($defense as $value=>$attributes) { should be foreach ($values as $value=>$attributes) { Quote Link to comment Share on other sites More sharing options...
Perad Posted December 12, 2007 Author Share Posted December 12, 2007 Thank you very much. Quote Link to comment 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.