[{" play":" hdr.1"," name":" 1"," year&#34 ;:" 1994"," class":" act bio deo"}, {"打":" hdr.2""名称":" 2""一年":&# 34; 1972"," class":" deo bio sil"}, {"打":" hdr.3""名称":" 3""一年":&# 34; 1974"," class":" sil moc tel"}, {"打":" hdr.5""名称":" 4""一年":&# 34; 1994"," class":" rep sim fal"}, {"打":" hdr.6""名称":" 5""一年":&# 34; 1967"," class":" viz tel moc"}, {"打":" hdr.7""名称":" 6""一年":&# 34; 2003"," class":" fal deo dec"}, {"打":" hdr.8""名称":" 7""一年":&# 34; 1999"," class":" tel act bio"}, {"打":" hdr.9""名称":" 8""一年":&# 34; 1993"," class":" mio moc viz"}, {"打":" hdr.10""名称":" 9""一年":&# 34; 1957"," class":" fal dec mio"}]
我有这个json.json数据,我使用这个PHP代码来制作我在某处使用的所有链接:
<?php
$fill = file_get_contents("json.json");
$tstJson = json_decode($fill);
foreach($tstJson as $val)
echo "<a class='".$val->class."' href='?".$val->play."' >".$val->name."</a>";
?>
但是我如何让它只回显包含&#34; act&#34; ? 就像排序模式一样。
答案 0 :(得分:0)
您只需if
preg_match
:
<?php
$fill = file_get_contents("json.json");
$tstJson = json_decode($fill);
foreach ($tstJson as $val) {
if (preg_match('/\bact\b/', $val->class)) {
echo "<a class='".$val->class."' href='?".$val->play."' >".$val->name."</a>";
}
}
?>
\bact\b
会检查整个单词是否为act
(例如,fact
不匹配)。
输出(带有换行符以便于阅读):
<a class='act bio deo' href='?hdr.1' >1</a>
<a class='tel act bio' href='?hdr.8' >7</a>
答案 1 :(得分:0)
您可以使用strpos()来检查是否采取了行动&#39;在回应之前在课堂上:
foreach($tstJson as $val)
if (strpos($val->class, 'act') !== false)
echo "<a class='".$val->class."' href='?".$val->play."' >".$val->name."</a>";
答案 2 :(得分:0)
试试这个
<?php
$fill = file_get_contents("json.json");
$tstJson = json_decode($fill);
foreach($tstJson as $val)
{
$act_class = explode(" ", $val->class);
if(in_array("act", $act_class))
{
echo "<a class='".$val->class."' href='?".$val->play."' >".$val->name."</a>";
}
}
?>