Combine Terms under same Title, unless new = create new Title Wordpress Query
I hope this helps.
$titles = array('Title 1 Apple Pie','Title 1 Cherry Pie','Title 2 Ham Pie','Title 3 Goat Pie');$list = '<ol>';foreach ($titles as $value) {$subtitle = substr($value, 0, 7);if(!strpos($final,$subtitle )){ $list .= '<li>' . $value . '</li>' ;} else { $pos = stripos($list, $subtitle); $left_string = substr($list,0, $pos); $substring = substr($list, $pos); $next_pos = stripos($substring, '<'); $right_string = substr($substring, $next_pos); $current = substr($substring, 8, $next_pos); $current = rtrim($current, '</li>'); $list = ''; $last = $current . ' '. substr($value, 8); $list = $left_string .' '. $subtitle.' '. $last . ' '. $right_string; } } $final .= '</ol>'; echo $final;
output.