php - Remove previous element from array if statement == true -
i have foreach loop iterates through array want check if array contains $item == 'survey-east-upper' , if that's true hide previous $item 'survey-east'. i've looked in_array
can't figure how remove previous element.
my original code:
foreach ($survey_array->services $service) { foreach ($service $item) { echo '<li title="' . rtrim($item) . '" class="' . strtolower(preg_replace('/[^a-za-z0-9]/', '-', rtrim($item))) . '">' . $item . '</li>'; } }
the new code:
foreach ($survey_array->services $service) { foreach ($service $item) { if (in_array("survey-east-upper", $survey_array->services)) { unset($site->services['survey-east']); } echo '<li title="' . rtrim($item) . '" class="' . strtolower(preg_replace('/[^a-za-z0-9]/', '-', rtrim($item))) . '">' . $item . '</li>'; } }
how can accomplish this?
dont use foreach, use indexing. in every iteration, 1 item ahead , check item. if "survey-east-upper", skip actual iteration , continue further.
foreach ($survey_array->services $service) { ($i = 0; $i < count($service) - 1; $i++) { $item = $service[$i]; if ($service[$i + 1] == "survey-east-upper") { continue; } echo '<li title="' . rtrim($item) . '" class="' . strtolower(preg_replace('/[^a-za-z0-9]/', '-', rtrim($item))) . '">' . $item . '</li>'; } }
edit:
you have last item in array $service[count($service) - 1]
, because wont included in loop
Comments
Post a Comment