Instead of working with numbers concatenated to identifiers, work with arrays, e.g.,
foreach ($joined as $i => $qty) {
echo "<form action='' method='post' name='yourForm'>";
echo "<button type='submit' name='remove[]' value='" . $i . "' class='deletebtn' >X</button>";
echo "</form>";
}
Notice that I have changed the form method from GET to POST. GET should not be used for forms that make some observable change to the user, i.e., typically they are used for forms that "get" data from the server. Since you're deleting by key and not value, it is only necessary to set the value to be the key (i.e., the index of the element_id) not the value (i.e., the element id itself). (Based on what you've written, I'm assuming that "element id" is itself some kind of value to be stored, rather than an actual id that you want to use as an index/key.)
You can then process the form like this:
if (isset($_POST['remove']) && is_array($_POST['remove'])) {
foreach ($_POST['remove'] as $index) {
if (is_numeric($index)) {
$index = (int)$index;
unset($_SESSION["quantity[$index]"]);
unset($_SESSION["element_ids"][$index]);
}
}
}
Similiarly, when you're constructing your session, you could do something like this:
if (isset($_POST['element_ids']) && is_array($_POST['element_ids'])) {
foreach ($_POST['element_ids'] as $index => $id) {
$_SESSION['element_ids'][$index] = $id;
}
}
assuming that the given form elements are named "element_ids[$i]" with value="some_id".