How to get first non empty filtered value from foreach PHP?
If I understood correctly, you want the first item from they array $items
that has not been emptied with your str_replace
.
If this is correct, here is how to proceed:
<?php
$items = array('a1a','bb2','o3e','two','three');
$first = true;
foreach($items as $item) {
$itemOriginal = $item;
$item = str_replace(['a1a', 'bb2', 'o3e'] ,'',$item); //filter remove
if(!empty($item) && $first == true){
$first = false;
echo "status publish -> ".$itemOriginal."
";
} else {
echo "skip -> ".$itemOriginal."
";
}
}
This will output:
skip -> a1a
skip -> bb2
skip -> o3e
status publish -> two
skip -> three
If this is not what you want, please clarify your question and add a comment so I can look at it again.
Update:
can value 'three' not skip? so i want to make wordpress publish using php from value foreach. the first value 'two' as publish and 'three' the next is as attachment
<?php
$items = array('a1a','bb2','o3e','two','three');
$first = true;
foreach($items as $item) {
$itemOriginal = $item;
$item = str_replace(['a1a', 'bb2', 'o3e'] ,'',$item); //filter remove
if(!empty($item) && $first == true){
$first = false;
echo "status publish -> ".$itemOriginal."
";
} elseif(!empty($item)) {
$first = false;
echo "status attachment -> ".$itemOriginal."
";
} else {
echo "skip -> ".$itemOriginal."
";
}
}
will output:
skip -> a1a
skip -> bb2
skip -> o3e
status publish -> two
status attachment -> three