I have an array then i filtered the value and the result is null/empty.
How to determine first filtered value that non empty?
<?php
$items = array('a1a','bb2','o3e','two','three');
$i=0;
foreach($items as $item) {
$item = str_replace('a1a','',$item); //filter remove
$item = str_replace('bb2','',$item); //filter remove
$item = str_replace('o3e','',$item); //filter remove
//ITRY THIS BUT NOT WORK IF THE SECOND AND THIRD IS EMPTY/NULL TOO
if($i==0) {
if(empty($item)){
//skip
} else {
//STATUS PUBLISH
}
} else {
//STATUS ATTACHMENT
}
$i++;
}
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