Jump to content

help with arrays pls


Jaret

Recommended Posts

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

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

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.