使用动态值PHP重复var

使用动态值PHP重复var,php,html,Php,Html,我有一定数量的帖子,每个帖子都包含一个模式(有点像弹出窗口)。这个模式的HTML在$HTML[]变量中。下面的代码运行良好,除非您看到它不是动态的 我所尝试的: $post_count = [1,2,3,4,5,6,7,8,9,10,11,12]; // i've to mannualy add a number according to the number of posts $counter = 1; foreach($number_of_modals as $modal){ ech

我有一定数量的帖子,每个帖子都包含一个模式(有点像弹出窗口)。这个模式的HTML在$HTML[]变量中。下面的代码运行良好,除非您看到它不是动态的

我所尝试的:

$post_count = [1,2,3,4,5,6,7,8,9,10,11,12]; // i've to mannualy add a number according to the number of posts
$counter = 1;
foreach($number_of_modals as $modal){
    echo $html[$counter];
    $counter++;
}
解释我需要完成的任务

$post_count; // if this val is 3 for example

echo $html[1];
echo $html[2];
echo $html[3];
你能试试这个吗

foreach($number_of_modals as $key => $modal){
    echo $html[$key];
}
因为您没有给出$number\u of_modals数组的结构,所以我假设它类似于$html数组

$modal = array('1', 'abc', 'etc');
转换为
$modal=array(0=>'1',1=>'abc',2=>'etc')


在foreach中,每个$key对应于0,1,2,这将给出您要查找的$counter。

正如Paul所建议的,使用for循环或while循环:

$post_count = 3;
$counter = 0;
while ($counter <= $post_count) {
    echo $html[$counter];
    $counter++;
}
$post\u count=3;
$counter=0;

虽然($counter,现在到底是什么问题?我想重复
$html
,重复次数与post\u count的次数相同。获取posts数量并按数组推送值。它将是动态的,例如posts数量=3,那么foreach循环将用于推送$post\u count数组中的值。@Paul为什么不做一个简单的for循环呢?谢谢Rizier123我想是这样的。谢谢,我接受了这是最好的答案,它按照我想要的方式工作。对不起,如果问题有点不清楚,请解释“我需要完成什么”第二部分几乎解释了应该是什么答案。几句话的解释会让这个例子更好。谢谢你的回答!@当然我添加了解释。不客气,保罗。