I have an array being looped with an if-else statement in it. The condition is if the name starts with a dot put the string in $gridNames
else put the string in $cssNames
. Somehow the last name that was valid for $gridNames
is being used for the rest of the loop although the condotion being false.
Here's the $selector["name"]
array:
string(4) ".row"
string(10) ".inner_row"
string(7) ".column"
string(13) ".inner_column"
string(12) "hey im a div"
string(8) "column 1"
string(5) "row 2"
And the code itself:
foreach ($element_css["element_css"] as $selector) {
$name = $selector["name"];
$lenght = strlen($name);
$firstLetter = substr($name, 0, -($lenght - 1));
if ($firstLetter == ".") {
$gridName = substr($name, 1, $lenght);
} else {
$nameByWords = explode(" ", $name);
foreach ($nameByWords as $word) {
$allWords .= $word;
}
$cssName = $allWords;
$allWords = "";
}
var_dump($gridName);
}
The var_dump
gives me:
string(3) "row"
string(9) "inner_row"
string(6) "column"
string(12) "inner_column"
string(12) "inner_column"
string(12) "inner_column"
string(12) "inner_column"
The result I want is:
string(3) "row"
string(9) "inner_row"
string(6) "column"
string(12) "inner_column"