Jump to content

array not passing into function correctly


TheTitans

Recommended Posts

I have an array that is set up like this:

 

Array ( [0] => 0 [1] => Happiness [2] => Stone )

 

Now I have my function:

 

public function calcEvolutionMethod($value) {
if ($value == 0) {
	return "Basic Form";
}
        else {
	return $value;
}
}

 

Each element in the array is used in the function.

"Basic Form" is returned for the first result, which is good; however "Basic Form" is returned for the second and third results too, which is not suppose to happen. Instead, for the second and third result, the else statement needs to kick in and just return "Happiness" and "Stone". I'm not sure what I'm doing wrong.

Yes.

 

for ($i = 0; $i < 1; $i++) {
$y2 = explode(",", $x2[$i]); // $y2 is the array that I posted above. The elements 0, Happiness, and Stone are in this array.
for ($j = 0; $j < 3; $j++) { // 3 elements in the array; we loop 3 times and display the results. "Basic Form" gets displayed three times, when it should only be displayed once
	echo $dex->calcEvolutionMethod($y2[$j])
}
}

Have you looked at what happens when you compare a string with a number:

 

$string = 'Happiness';
if ($string == 0) {
   echo 'Match';
} else {
   echo 'No Match';
}

 

$string = 'Happiness 4 all';
if ($string == 4) {
   echo 'Match';
} else {
   echo 'No Match';
}

 

$string = 'Happiness';
if ($string === 0) {
   echo 'Match';
} else {
   echo 'No Match';
}

 

Can tidy your code up a lot- by the way using foreach () to loop through an array is much easier! I don't quite know what you were trying to do with the first for (), perhaps just loop once as there's only one entry in the array? Remember a loop is for repeating code, could have just used: $y2 = explode(",", $x2[0]);.. But I've changed the second for () as an example to show you..

 

for ($i = 0; $i < 1; $i++) {
    // $y2 is the array that I posted above. The elements 0, Happiness, and Stone are in this array.
    $y2 = explode(",", $x2[$i]);

    // 3 elements in the array; we loop 3 times and display the results. "Basic Form" gets displayed three times, when it should only be displayed once
    foreach ($y2 as $val) {
        echo $dex->calcEvolutionMethod($val);
    }
}

 

Try changing this line to:

 

echo $val . ': ' . $dex->calcEvolutionMethod($val);

 

So you know it's definitely getting the right values passed..

 

Adam

Found the error; weird error too.

 

if ($value == 0) {}

 

...That had to be written as:

 

if ($value == "0") {}

 

MrAdam: I know about the foreach statement. The reason why I didn't use it is because there's other code in there. I didn't include it because it was basic HTML and SQL queries that weren't necessary to show.

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.