I have an error where I’m not supposed to.
I’m working with Symfony and need to separate information from a string.
The error is:
Warning: Undefined array key 2
I get a string like this:
$buffer = "y;;x;x;x;x;x;x;x;x;;y"
I want to separate each x and each y in a variable so I splited the string in 3 by “;;” and the middle part in 8 by “;”. Like this:
// get all information out of the string
$separation = explode(";;", $buffer);
$usernameToAdd = $separation[0];
$others = $separation[1];
$notifRule = $separation[2];
// get all from others
$allFromOthers = explode(';', $others);
$var1 = $allFromOthers[0];
$var2 = $allFromOthers[1];
$var3 = $allFromOthers[2];
$var4 = $allFromOthers[3];
$var5 = $allFromOthers[4];
$var6 = $allFromOthers[5];
$var7 = $allFromOthers[6];
$var8 = $allFromOthers[7];
I got the error on key 2 of ‘$allFromOthers’ but if I switch lines I’ll get an error on the upper key but never on key 0 or 1.
I tried to ‘dd’ my variables but everything was ok. I even noticed the strange error that lead me to the title of my post: if I put the ‘dd’ after the line with the error, everything works and I got my data on the screen, where I can see that my code works well. I don’t know how this is possible so if someone can help me with this it would be nice!