1

I'm using this function:

function makeImmunities($data) {
    $immunities = explode(',', $data);
    foreach($immunities as $immunity) {
        switch($immunity) {
            case 'poison':
                $ret .= '<img src="/images/gems/earth.gif"/> ';
            break;
            case 'earth':
                $ret .= '<img src="/images/gems/earth.gif"/> ';
            break;
            case 'paralyze':
                $ret .= '<img src="/images/gems/paralyze.gif"/> ';
            break;
            case 'death':
                $ret .= '<img src="/images/gems/death.gif"/> ';
            break;
            case 'ice':
                $ret .= '<img src="/images/gems/ice.gif"/> ';
            break;
            case 'invisible':
                $ret .= '<img src="/images/gems/invisible.gif"/> ';
            break;
        }
    }
    return $ret;
}

And $ret has .=, so content should be added to itself. But it doesn't. I wan't it to work this way:

$data has an array which looks like this : 'poison', 'earth', 'death'. And this function returns only first case:

$ret .= '<img src="/images/gems/earth.gif"/> ';

I wan't it too add ALL of the content that $ret has if case is the same as the $immunity.

7 Answers 7

2

Switch doesn't work like that you will need to use an if and do something like this:

$options = array(
    'poison' => '<img src="/images/gems/earth.gif"/> ',
    'earth'  => '<img src="/images/gems/earth.gif"/> '
    'etc'    => '...'
);

if(isset($options[$immunity])){
    $ret .= $options[$immunity];
}
Sign up to request clarification or add additional context in comments.

2 Comments

Of course the switch works like that. You didn't understand the question, did you? ;)
@jacek switch may work like that, but regardless this is a superior solution to using switch.
1

The most extensible and easiest I can find (without an if or even a loop)

function makeImmunities($data) {

   $allImmunities = array(
     'poison' => '/images/gems/earth.gif',
     'earth' => '/images/gems/earth.gif',
     'paralyze' => '/images/gems/paralyze.gif',
     'death' => '/images/gems/death.gif',
     'ice' => '/images/gems/ice.gif',
     'invisible' => '/images/gems/invisible.gif',
   );

   $immunities = array_intersect_key($allImmunities, array_flip(explode(',', str_replace(' ','',$data))));

   return implode(' ', array_map(function(&$item, $key) {
      return "<img src=\"{$item}\" alt=\"{$key}\" />";
   }, $immunities, array_keys($immunities)));
}

Exemple:

var_export(makeImmunities('ice,poison,death'));

Output

'<img src="/images/gems/earth.gif" alt="poison" /> <img src="/images/gems/death.gif" alt="death" /> <img src="/images/gems/ice.gif" alt="ice" />'

Comments

1

You wrote that $data contains an array, but from the code it is clear, that it should contain the string, for example $data='earth,ice,poison';. Notice, that there mustn't be any space. By the way, it is good to initialize (with empty string) the $ret variable before foreach.

Comments

1

Just do it like this:

$immunities = explode(',', $data);
foreach($imminities as $key => $value) {
    $ret .= "<img src='/images/gems/{$value}.gif" />";
}
return $ret;

You'll have to check for the immunities though. You could easily add a if clause to inside the foreach, something like

if($value == "earth" || $value == "water" ....)

Pretty simple way to do it.

Update As suggested by Cole, the part inside the if condition could be replaced with a in_array($value, $immunities_supported). I don't know about the efficiency of this though. Remember you also have to add a $immunities_supported = array("earth", "water"); etc with the immunities that are present. (The code below by Cole doesn't work directly though, it's just a concept, since $value is always in $immunities)

2 Comments

For the latter, you could do if(in_array($value,$immunities)).
Oh right. Though, I try to avoid in_array and these looping things as much as possible, because if $immunities becomes large, an in_array would potentially use a lot of resources. Though that huge if might also use it... Eh. Weak argument, I know.
0

Firstly, you need to initialize your "$ret" variable, add "$ret='';" before the "foreach" loop. Use if and elseif.

2 Comments

You don't need to initalize the $ret. You should, but you don't have to - foreach doesn't have its own scope, so the variable is created in the first iteration and it is persistent.
@jacek If you want to code correctly, you need it. But if you want to make dirty codes, as it seem to be your case, you don't need it, I agree with you.
0

function makeImmunities($data) {

$ret = ""; //initialise $ret

$immunities = explode(',', $data);

foreach($immunities as $immunity) {

    switch($immunity) {
        case 'poison':
            $ret .= '<img src="/images/gems/earth.gif"/> ';
        break;
        case 'earth':
            $ret .= '<img src="/images/gems/earth.gif"/> ';
        break;
        case 'paralyze':
            $ret .= '<img src="/images/gems/paralyze.gif"/> ';
        break;
        case 'death':
            $ret .= '<img src="/images/gems/death.gif"/> ';
        break;
        case 'ice':
            $ret .= '<img src="/images/gems/ice.gif"/> ';
        break;
        case 'invisible':
            $ret .= '<img src="/images/gems/invisible.gif"/> ';
        break;
    }
    /*
     * creates an array $match with key = immunity of match found e.g poison, death, earth
     * and values equal to <img src="/images/gems/xxxxxx.gif"/> where xxxxxx is the 
     * value for each corresponding image tag. 
     */
    $match[$immunity] = $ret; 
}
return $match;

}

$input = 'poison, earth, death';

$ans = makeImmunities($input); // returns an array

print_r($ans); //prints the array

?>

Comments

0
function makeImmunities($data) {

$ret = ""; //initialise $ret

$immunities = explode(',', $data);  
foreach($immunities as $immunity) {

    switch($immunity) {
        case 'poison':
            $ret .= '<img src="/images/gems/earth.gif"/> ';
        break;
        case 'earth':
            $ret .= '<img src="/images/gems/earth.gif"/> ';
        break;
        case 'paralyze':
            $ret .= '<img src="/images/gems/paralyze.gif"/> ';
        break;
        case 'death':
            $ret .= '<img src="/images/gems/death.gif"/> ';
        break;
        case 'ice':
            $ret .= '<img src="/images/gems/ice.gif"/> ';
        break;
        case 'invisible':
            $ret .= '<img src="/images/gems/invisible.gif"/> ';
        break;
    }

    $match[$immunity] = $ret; 
}
return $match;

}

1 Comment

this modified function accepts data in form of $data = 'poison, death, earth'; i.e a string separated by ',' then it returns an array. so you can use print_r() to print it or treat the returned value as array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.