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; } } } } Link to comment https://forums.phpfreaks.com/topic/81328-solved-dysfunctional-foreach-statement/ 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. Link to comment https://forums.phpfreaks.com/topic/81328-solved-dysfunctional-foreach-statement/#findComment-412768 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 Link to comment https://forums.phpfreaks.com/topic/81328-solved-dysfunctional-foreach-statement/#findComment-412769 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 Link to comment https://forums.phpfreaks.com/topic/81328-solved-dysfunctional-foreach-statement/#findComment-412771 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 Link to comment https://forums.phpfreaks.com/topic/81328-solved-dysfunctional-foreach-statement/#findComment-412775 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 ) ) ) Link to comment https://forums.phpfreaks.com/topic/81328-solved-dysfunctional-foreach-statement/#findComment-412777 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) { Link to comment https://forums.phpfreaks.com/topic/81328-solved-dysfunctional-foreach-statement/#findComment-412780 Share on other sites More sharing options...
Perad Posted December 12, 2007 Author Share Posted December 12, 2007 Thank you very much. Link to comment https://forums.phpfreaks.com/topic/81328-solved-dysfunctional-foreach-statement/#findComment-412785 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.