I'm using a foreach loop to go through my array. I'm making changes to specific values, but leaving alone the rest. (In this case, I'm making a break before every item with a colon, unless it's the first one)
If the current value contains a specific string, I want to remove that value and the value immediately after it within the array and continue from there in the foreach loop.
For example:
$items = array( 'Color:', 'blue', 'Size:', '12" x 12"', 'Weight:', '5 lbs' );
$first = true;
foreach ($items as $item) {
if ((strpos($item, ':')) && $first) {
$item = '<b>' . $item . '</b>';
$first = false;
} elseif (strpos($item, ':')) {
$item = '<br>' . '<b>' . $item . '</b>';
}
if (strpos($item, 'Size:')) {
// Remove this item (Size) and the one directly after it (12" x 12")
}
}
This is what I'd like to return:
$items = array( 'Color:', 'blue', 'Weight:', '5 lbs' );
Since different items have a variety of values, which won't necessarily be in the same order every time, I can't select by item[2]
and item[3]
, etc.