I am trying to figure out how I can use a PHP array inside of a PHP function. I was able to use string replace to replace {count} with the $counter variable inside of the function. But I am unable to do the same thing to a array that is inside the string. I tried using $i
from the For loop to select the arrays index, but that did not work. I also tried using {count}
for the array index and then used string replace to replace it with the $counter
variable. That did not work also. If anyone can point in the right direction, I would apperciate it. Thank you for your time.
<?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>');
?>
Output Example:
<div class="image-1">orange</div>
<div class="image-2">apple</div>
<div class="image-3">grape</div>
<div class="image-4">watermelon</div>
I don't know why would you need this, but you can do something like this:
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 );
If you want to use an array in a function, you have to set it as a separate attribute.
Now you don't need to use the $repeatCount
attribute, if you want to go through te entire array.
EDIT:
You can also make your own "template" by combining HTML and PHP.
<?php
$content = array( 1 => 'orange', 'apple', 'grape', 'watermelon' );
foreach ( $content as $key => $value )
{
?> <div class="image-<?=$key?>"><?=$value?></div> <?php
}
I would recommend using an existing template engine, though. This code is not as clear and it could become really messy.
I think you are having more problems in your code. You do not need to replace anything. You can define and call your function like this:
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
I cleaned it up a bit and wrote a general function to help you with arrays. Please note that I agree this may not necessarily be the best option - but it does solve your problem the way you wanted it to.
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);
This outputs <div class="image-1">orange</div><div class="image-2">apple</div><div class="image-3">grape</div><div class="image-4">watermelon</div>
, you could easily add eg a newline or a <br>
in the echo statement if you need a blank line.
If you need any help explaining how the function works, please let me know.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.