Reputation: 33
Alright, I'm a little lost. Here's my code, but how do I get rid of the PHP error that tells me it's an illegal offset type? I'm trying to cycle through the colors.
$color = array(
[0]=>"red",
[1]=>"cherry",
[2]=>"orange",
[3]=>"amber",
[4]=>"blue",
[5]=>"sapphire",
[6]=>"green",
[7]=>"forest green",
[8]=>"purple",
[9]=>"lavender");
//starts at index 0
for($colorCount=0; $colorCount <=9; $colorCount++){
if ($colorCount == 9){
break;
}
echo implode(", ", $color).", ";
}
Upvotes: 0
Views: 263
Reputation: 787
$color = array(
'0'=>"red",
'1'=>"cherry",
'2'=>"orange",
'3'=>"amber",
'4'=>"blue",
'5'=>"sapphire",
'6'=>"green",
'7'=>"forest green",
'8'=>"purple",
'9'=>"lavender"
);
foreach ($color as $key => $res) {
print_r($res);
}
Upvotes: 1
Reputation: 57709
I think what might be happening is that, since PHP now supports short-array notation with []
. It's parsing [0]
as array(0)
which you then try to use as a key in an array, which is not allowed. This would explain that exact error message.
Declare your array like:
$color = array(
0 => "red",
1 => "cherry",
2 => "orange",
3 => "amber",
4 => "blue",
5 => "sapphire",
6 => "green",
7 => "forest green",
8 => "purple",
9 => "lavender"
);
You can even leave off the numbers and do:
$color = array(
"red",
"cherry",
"orange",
"amber",
"blue",
"sapphire",
"green",
"forest green",
"purple",
"lavender"
);
Upvotes: 1