Question

There must be an obvious bug in this code but I'm not seeing it. Mind taking a look?

The below code returns

string
fleet
Warning: Illegal offset type (line 6)    

The taskforces subroutine just pulls an .ini file, reads it into an array, and returns the array, which the foreach then iterates through. In relevant part, the array looks like this.

; this is an INI file
[scout]
type = "fleet"

Here is the code:

foreach($_SESSION['ini']->taskforces() as $key => $val)
{
    echo gettype($val["type"]);
    echo $val["type"];

    if($val["type"] == "fleet") {
        $commanderData[$val] = "BLOB";
        $commanderData["sc$val"] = "INT NOT NULL";
    }
}

I'd like to not have the illegal offset type, because I want the code to go through to the if condition. What obvious thing am I missing?

Thanks.

Was it helpful?

Solution 2

I'm not sure why this caused the problem, but I realized that the result of the if statement was not correct. The code

$commanderData[$val] = "BLOB";

attempts to use the matrix $val as the key for the $commanderData array. It should use the string $key from the iteration through the ini file. Once fixed, I stopped getting the warning, but it's not clear why this would have thrown the error on the proceeding line.

OTHER TIPS

Instead of this:
echo $val["type"];
you should have simply:
echo $val;
Just because $val is not an array, it's a string. You've made a foreach through an array, so on each iteration you get an array key and an array value (which is, obviously, the string "fleet").

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top