[英]Special treatment to part of PHP array
I want to add a special class to the a certain entry of an array value.我想向数组值的某个条目添加一个特殊的类。 What do I need to add to that value in the array to give it's 'li' a special class?
我需要向数组中的那个值添加什么才能给它的 'li' 一个特殊的类?
I have an array like this:我有一个这样的数组:
"notes" => [
"$100.00 Credit to Use at Any of",
"the Domestic Bliss Spa Locations",
"$100.00 Value",
"Another list item",
"yet another list item"
]
And I loop through these to spit out html like this:我循环遍历这些以像这样吐出 html:
<?php foreach ($slide['notes'] as $note): ?>
<li><?= $note; ?></li>
<?php endforeach; ?>
I want the value $100.00 Value
to have a special class.我希望价值
$100.00 Value
有一个特殊的班级。
Try this:尝试这个:
<?php foreach ($slide['notes'] as $note): ?>
<li <?php if ($note == "$100.00 Value") echo "class='specialClass'" ?>><?= $note; ?></li>
<?php endforeach; ?>
Here you can check for multiple values like this way as :在这里,您可以像这样检查多个值:
<?php
foreach ($slide['notes'] as $note) {
$values = array("$300.00", "$200.00", "$100.00", "$55.00");
if (in_array($note, $values))
{
echo "<li class='special_class_name'>$note</li>";
} else {
echo "<li>$note</li>";
}
}
?>
You can use strpos to check if $note
contains $100.00 Value
and use the ternary operator , like this:您可以使用strpos检查
$note
包含$100.00 Value
并使用三元运算符,如下所示:
<?php
foreach ($slide['notes'] as $note){
echo (strpos($note, '$100.00 Value') !== false) ? "<li class='someClass'>{$note}</li>" : "<li>{$note}</li>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.