在PHP函数中使用PHP数组?

在PHP函数中使用PHP数组?,php,arrays,string,function,Php,Arrays,String,Function,我试图弄清楚如何在PHP函数中使用PHP数组。我能够在函数内部使用stringreplace将{count}替换为$counter变量。但是我无法对字符串中的数组执行相同的操作。我尝试使用For循环中的$I来选择数组索引,但没有成功。我还尝试对数组索引使用{count},然后使用stringreplace将其替换为$counter变量。这也不起作用。如果有人能指出正确的方向,我会安抚它。谢谢你抽出时间 <?php function repeatHTML($repeatCount,

我试图弄清楚如何在PHP函数中使用PHP数组。我能够在函数内部使用stringreplace将{count}替换为$counter变量。但是我无法对字符串中的数组执行相同的操作。我尝试使用For循环中的
$I
来选择数组索引,但没有成功。我还尝试对数组索引使用
{count}
,然后使用stringreplace将其替换为
$counter
变量。这也不起作用。如果有人能指出正确的方向,我会安抚它。谢谢你抽出时间

<?php
    function repeatHTML($repeatCount, $repeatString){
        $counter = 1;
        for ($i = 1; $i <= $repeatCount; $i++) {
            $replacedRepeatString = str_replace('{count}', $counter, $repeatString);
            echo $replacedRepeatString;
            $counter++;
        }   
    }

    $titleContent = array('orange', 'apple', 'grape', 'watermelon');

    repeatHTML(4, '<div class="image-{count}">'.$titleContent[$i].'</div>'); 
?>

我不知道你为什么需要这个,但你可以这样做:

function repeatHTML( $repeatHTML, $repeatArray ) {
    foreach ( $repeatArray as $key => $repeatValue ) {
        $replacedRepeatString = str_replace('{count}', $key, $repeatHTML);
        $replacedRepeatString = str_replace('{value}', $repeatValue, $repeatHTML);
        echo $replacedRepeatString;
    }
}

// 1 => only if you want to start from 1, instead of 0
$titleContent = array( 1 => 'orange', 'apple', 'grape', 'watermelon' );

repeatHTML( '<div class="image-{count}">{value}</div>', $titleContent );
function repeatHTML($arr){ // add array as parameter
   $content = ""; // initialize variable
   $i = 0; // initialize variable
   foreach($arr as $k=>$n){ // loop through array
     $content .= '<div class="image-'.$i.'">'.$n.'</div>'; // fill variable with html
     $i++; // increment counter variable
   }
   return $content;
}

$titleContent = array(0=>'orange', 1=>'apple', 2=>'grape', 3=>'watermelon'); // fill array
echo repeatHTML($titleContent); // call function

不过,我建议使用现有的模板引擎。此代码不够清晰,可能会变得非常混乱。

我认为您的代码中存在更多问题。你不需要更换任何东西。您可以这样定义和调用函数:

function repeatHTML( $repeatHTML, $repeatArray ) {
    foreach ( $repeatArray as $key => $repeatValue ) {
        $replacedRepeatString = str_replace('{count}', $key, $repeatHTML);
        $replacedRepeatString = str_replace('{value}', $repeatValue, $repeatHTML);
        echo $replacedRepeatString;
    }
}

// 1 => only if you want to start from 1, instead of 0
$titleContent = array( 1 => 'orange', 'apple', 'grape', 'watermelon' );

repeatHTML( '<div class="image-{count}">{value}</div>', $titleContent );
function repeatHTML($arr){ // add array as parameter
   $content = ""; // initialize variable
   $i = 0; // initialize variable
   foreach($arr as $k=>$n){ // loop through array
     $content .= '<div class="image-'.$i.'">'.$n.'</div>'; // fill variable with html
     $i++; // increment counter variable
   }
   return $content;
}

$titleContent = array(0=>'orange', 1=>'apple', 2=>'grape', 3=>'watermelon'); // fill array
echo repeatHTML($titleContent); // call function
函数repeatHTML($arr){//添加数组作为参数
$content=”“;//初始化变量
$i=0;//初始化变量
foreach($arr as$k=>$n){//循环遍历数组
$content.=''.$n.';//用html填充变量
$i++;//递增计数器变量
}
返回$content;
}
$titleContent=array(0=>'orange',1=>'apple',2=>'grape',3=>'西瓜');//填充数组
echo repeatHTML($titleContent);//调用函数

我对它进行了一些清理,并编写了一个通用函数来帮助您使用数组。请注意,我同意这不一定是最好的选择,但它确实能以您希望的方式解决您的问题

function repeatHTML($count, $string, $array){
    foreach ($array as $index => $value) {
        echo str_replace(['{count}', '{value}'], [$index + 1, $value], $string);
    }
}

$titleContent = array('orange', 'apple', 'grape', 'watermelon');
repeatHTML(4, '<div class="image-{count}">{value}</div>', $titleContent);
函数repeatHTML($count,$string,$array){
foreach($index=>$value的数组){
echo str_replace(['{count}','{value}',[$index+1,$value],$string);
}
}
$titleContent=数组('orange'、'apple'、'grape'、'西瓜');
repeatHTML(4,{value}',$titleContent);
这将输出
OrangeAppleGrapeTermelon
,如果需要空行,您可以轻松地在echo语句中添加换行符或


如果您需要解释函数如何工作的任何帮助,请告诉我。

我不确定您想要什么,请发布您希望示例的输出是什么?我添加了输出示例。抱歉,再次更新了它。如果我不想在函数中包含数组,因为字符串中可能使用任意数量的数组,该怎么办。我想这样做,因为我重复了这么多HTML代码,这会节省很多时间。你可以在这个函数中使用任何你想要的数组。对于重复的HTML代码,更好的解决方案是使用模板引擎。举个例子,或者想起来。看看他们,谢谢。我还是想弄清楚,不过,它现在会派上用场的。有时我会建立一些简单的信息网站,重复这么多代码是一种痛苦。我编辑了我的答案。您可以使用PHP+HTML组合。不过,我不推荐使用它,最好使用细枝模板。这就好像你正在使用带有PHP函数的HTML。是的,我也有过类似的工作。我只是尽量不把数组放在函数中,这样我就可以在字符串中有我想要的任意多个数组。我觉得我解释得不够好。我希望能够输入字符串(字符串是需要重复的HTML代码)。我希望能够输入一个字符串,它可以包含数组,并且数组的索引是根据for循环计算的。我在几年前就这样做了。我认为这是一个糟糕的做法。您将最终成为html和php的混合体。把观点和逻辑分开!理解。可能是为什么另一个人建议使用模板引擎。谢谢你的意见,我还是想弄明白!无论如何,这样我就不必在函数中包含数组了?对于我想重复的东西,我可以有很多数组。你是说如果你有
array('orange','apple',array('grape','西瓜'))
等等?或者你的意思是你不总是发送数组?如果我有这样的东西:{value}{value-2}看看怎么会有很多值?这就是为什么如果我将数组放在函数中,那么我只能使用一个数组。如何支持多个不同的数组?如果要向函数发送动态数量的数组,这可能会成为一个严重的难题。很抱歉,但我认为我无法为您提供一个好的解决方案。我希望你能找到一个好答案——祝你好运!一切都好。我能够想出你的解决方案,我只是不知道如何做动态数组部分。我想我会继续找的,谢谢你的帮助。