Jaret Posted December 15, 2009 Share Posted December 15, 2009 I spent whole day trying to figure this out but I'm too bad with arrays and statements, so I had to call for your help. I have an array which I get from a MySQL query: Array ( [id] => 1 [skill_speed] => 1 [skill_melee] => 1 [skill_kick] => 1 [skill_block] => 0 ) I need to list only the skills with values larger than 0. Without other values like [id] or [skill_block] which is 0. Need to have this output: Skill Speed Skill Melee Skill Kick I figure I need to use IF, FOREACH and wildcard characters but I can't make it work in an efficient way. Link to comment https://forums.phpfreaks.com/topic/185230-help-with-arrays-pls/ Share on other sites More sharing options...
cags Posted December 15, 2009 Share Posted December 15, 2009 foreach($array as $k=>$v) { if($v > 0) { echo $k; } } Link to comment https://forums.phpfreaks.com/topic/185230-help-with-arrays-pls/#findComment-977816 Share on other sites More sharing options...
Adam Posted December 15, 2009 Share Posted December 15, 2009 If the index names you wish to list always have the "skill_" prefix, you can use a foreach loop as you said: foreach ($array_name as $key => $skill) { if (substr($key, 0, 6) == 'skill_' && $skill != 0) { echo $skill.'<br />'; } } As writing that though I noticed you wanted to use a label that wasn't stored in the array, which is a problem. Although you could still make this work by creating an array of labels, associated by the array key returned from the query -- if that makes sense? This may be a little over-doing it though for just 3 or 4 values. Instead you could just use a series of if statements.. if ($array_name['skill_speed'] != 0) echo 'Skill Speed ' . $array_name['skill_speed'] . '<br />'; (...) Link to comment https://forums.phpfreaks.com/topic/185230-help-with-arrays-pls/#findComment-977819 Share on other sites More sharing options...
Jaret Posted December 15, 2009 Author Share Posted December 15, 2009 Thank you so much guys!!! Link to comment https://forums.phpfreaks.com/topic/185230-help-with-arrays-pls/#findComment-977836 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.