'PHP 1 image for every 5 amount
I need to assign 1 image to every 5 people in the array using foreach and a forloop
<?php
$clubs["Example 1"] = 25;
$clubs["Example 2"] = 12;
$clubs["Example 3"] = 34;
$clubs["Example 4"] = 10;
foreach()
does anyone have suggestions?
Solution 1:[1]
Result: https://extendsclass.com/php-bin/b431455
<?php
$personsPerIcon = 5;
$icon = [
"url" => "https://cdn-icons-png.flaticon.com/512/456/456212.png",
"size" => "15px"
];
$clubs = [
[
"name" => "Manchester",
"persons" => 25,
], [
"name" => "PSG",
"persons" => 12
], [
"name" => "Madrid",
"persons" => 34
], [
"name" => "Bayern",
"persons" => 10
]
];
foreach ($clubs as $i => $club) {
$clubs[$i]["icons"] = floor($club["persons"] / $personsPerIcon);
}
?>
<ul>
<?php foreach ($clubs as $club) : ?>
<li>
<?= str_repeat(
'<img src="' . $icon["url"] . '" width="' . $icon["size"] . '">',
$club["icons"]
) ?>
<?= $club["name"] . ' (' . $club["persons"] . ') ' ?>
</li>
<?php endforeach ?>
</ul>
Based on @CBroe comment
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
