如何在一组数组中循环条件以返回 true 的值


How to cycle a condition through a set of arrays to return the values of the one that is true

我有一个文本区域,它提交的文本使用爆炸函数转换为数组,然后将该数组的值转换为foreach循环中的单个数组。这是函数

文本区域条目
周一|0000|0500|晨曦|莎莉.jpg|DJ 莎莉

--- 周一|0500|1000|日间肥皂|沃利.jpg|沃利

处理代码

$itemlist = trim(preg_replace('/'n/', '', $itemlist ));
$itemlist = explode('---', $itemlist);
$dow = strtolower(date( 'D' ));
$tod = date( 'Hi' );
foreach($itemlist as $items) {
//this creates 2 arrays with 6 keys each
//array([0]=>mon[1]=>0000[2]=>0500[3]=>The Morning Blast[4]=>sally.jpg[5]=>DJ Sally)
//array([0]=>mon[1]=>0500[2]=>1000[3]=>Day Time Soap[4]=>wally.jpg[5]=>Wally)
$itempart = explode('|', $items);
if($itempart[0] == $dow && $tod >= $itempart[1] && $tod <= $itempart[2] ) {
?>
<div>
<h4><?php echo $itempart[3]; ?></h4>
<p><?php echo $itempart[5]; ?></p>
<img src="<?php echo $itempart[4]; ?>" />
</div>
<?php } 
}//endforeach ?>

问题
所在检查星期几和一天中的时间的条件,仅处理第一个数组并正确返回输出 HTML,但是一旦时间到期并且它为 false,即使它是 true,也不会处理后续数组。

逻辑似乎是正确的,但它就是行不通。需要什么才能使此扫描成为数组列表并仅返回正确的数组?

该代码不完整:循环已打开但从未完成。您的问题是您只是修剪',同时您还必须期望其他字符(回车符,而不仅仅是换行符)。正确消除换行符

$itemlist= trim( preg_replace( '/''s*[''n''r]''s*/', '', $itemlist ) );

或稍后修剪

$itempart= explode( '|', trim( $items ) );

否则,条件将失败,因为数组元素中的fri前面带有回车符。