Assuming a list like this:
$array = array('item1', 'item2', 'item3'); // etc...
I would like to create a comma separated list like so:
implode(',', $array);
But have the added complication that I'd like to use the following logic: if the item index is a multiple of 10, use ',<br>'
instead of just ','
for the separator in the implode()
function.
What's the best way to do this with PHP?
I did this like so, but wonder if there's a more concise way?
function getInventory($array, $title) {
$list = array();
$length = count($array);
$i = 1;
foreach($array as $item) {
$quantity = $item[1];
if(!$quantity)
$quantity = 1;
$item_text = $quantity . $item[3];
if($i > 9 && ($i % 10) == 0) {
$item_text .= ',<br>';
} elseif($i !== $length) {
$item_text .= ',';
}
$list[] = $item_text;
$i++;
}
$list = implode('', $list);
$inventory = $title . $list . '<br>';
return $inventory;
}