I have this array
$arr= [
0 => "T"
1 => "h"
2 => "e"
3 => " "
4 => "w"
5 => "r"
6 => "i"
7 => "t"
8 => "e"
9 => "r"
10 => ""
11 => " "
12 => "w"
13 => "a"
14 => "s"
15 => " "
..
..
for($j=0;$j<count($arr);$j++)
{
if($arr[$j-1]==" ")
{
}
}
In this if condition if($arr[$j-1]==" ")
getting this error
message: "Undefined offset: -1", exception: "ErrorException
the previous key exists but i’m still getting error
Any solution to fix this issue
2
Answers
Index Starts at zero
You are trying to check if the previous key is a space, do something to current key!
$j
Is starting form zero. so you are checking the index$j-1
.To achieve what you want, you should start form
1
tocount($arr)
it self.Try code below:
Or this:
Or Even this: