在PHP中匹配关键字文本的数组子集中循环
我有这个数组(使用PHP): 我只想循环菜单_趋势*值,并获得如下列表:在PHP中匹配关键字文本的数组子集中循环,php,arrays,transformation,Php,Arrays,Transformation,我有这个数组(使用PHP): 我只想循环菜单_趋势*值,并获得如下列表: <ul> <li>Text 01: 248714</li> <li>Text 02: 248680</li> <li>[...]</li> </ul> 文本01:248714 文本02:248680 [……] 你能推荐最好的方法吗?我不确定这是不是最好的方法,但它会起作用: $output = array(); fore
<ul>
<li>Text 01: 248714</li>
<li>Text 02: 248680</li>
<li>[...]</li>
</ul>
- 文本01:248714
- 文本02:248680
- [……]
你能推荐最好的方法吗?我不确定这是不是最好的方法,但它会起作用:
$output = array();
foreach ($array as $k => $a) {
if (stristr($k, 'menu_trend_') && !empty($arr[$k . '_txt'])) {
$output[] = $arr[$k . '_txt'] . ': ' . $a;
}
}
echo "<ul>\n<li>" . implode("</li>\n<li>", $output) . "</li>\n</ul>";
$output=array();
foreach($k=>a的数组){
if(stristr($k,'menu_trend')&&&!empty($arr[$k.''u txt'])){
$output[]=$arr[$k.\u txt'].:'.$a;
}
}
回声“\n- ”。内爆(“
\n- ”,$output)。“
\n
”;
您可以使用它,它将尝试匹配菜单趋势(数字),如果匹配,将回显所需的文本
echo '<ul>';
foreach ($array as $key => $val) {
$matches = array();
if (!preg_match('/^menu_trend_(\d+)$/', $key, $matches)) {
continue;
}
echo sprintf('<li>Text %s: %s</li>', $matches[1], $val);
}
echo '</ul>';
echo'';
foreach($key=>$val的数组){
$matches=array();
如果(!preg_match(“/^menu_trend”(\d+$/”,$key,$matches)){
继续;
}
echo sprintf(“- 文本%s:%s
”,$matches[1],$val);
}
回声“
”;
您使用的是哪种语言?这将返回一个策略列表:- Text 01:248714
- Text 01:Text 01
- Text 02:248680
- Text 02:Text 02
- 抱歉,我忘了终止regex规则,现在尝试一下
echo '<ul>';
foreach ($array as $key => $val) {
$matches = array();
if (!preg_match('/^menu_trend_(\d+)$/', $key, $matches)) {
continue;
}
echo sprintf('<li>Text %s: %s</li>', $matches[1], $val);
}
echo '</ul>';