I've been having considerable trouble trying to figure out why my arrays weren't working as expected. I was using code functionally the same as the code below, but it was silently failing on me in my program, so I wrote an isolated test case using the same types of data and syntax and got the errors about illegal offset types.
Warning: Illegal offset type in <file location>\example.php on line 12
Warning: Illegal offset type in <file location>\example.php on line 16
Those refer to the two lines containing the reference to "$questions[$question]" specifically.
<?php
$questions = array(
"訓読み: 玉"=>array("たま","だま"),
"訓読み: 立"=>array("たて","たち","たつ","たてる","だてる","だて"),
);
$question = $questions["訓読み: 立"];
if (is_array($questions[$question])){
$res = $questions[$question][0];
} else {
$res = $questions[$question];
}
echo $res;
?>
I think I'm just beyond my skill level here, because while I can see the warning on http://php.net/manual/en/language.types.array.php that states "Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.", I cannot see how what I'm doing is any different than Example #7 on that very page.
I would greatly appreciate an explanation that would help me understand and solve my problem here.
Thank you in advance!
When you call $question = $questions["訓読み: 立"];
, you are receiving the array represented by that string. When you use $questions[$question], you should just be using $question:
<?php
$questions = array(
"訓読み: 玉"=>array("たま","だま"),
"訓読み: 立"=>array("たて","たち","たつ","たてる","だてる","だて"),
);
$question = $questions["訓読み: 立"];
if (is_array($question)){
$res = $question[0];
} else {
$res = $question;
}
echo $res;
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With