Here are some ways to do this:
$items = ["Bhir", "Ekky", null, "Uych", "foo"=>"bar"]; $values = array_values($items); // Bhir, Ekky, Uych, bar foreach ($values as $i => $item) { print("$item"); $next = isset($values[$i + 1]); if ($next) { print(", "); } } // Bhir, Ekky, , Uych, bar foreach ($values as $i => $item) { print("$item"); $next = array_key_exists($i + 1, $values); if ($next) { print(", "); } } // Bhir, Ekky, , Uych, bar $i = count($values); foreach ($items as $item) { print("$item"); $next = !!(--$i); if ($next) { print(", "); } } // Bhir, Ekky, , Uych, bar $items = new \CachingIterator(new \ArrayIterator($items)); foreach ($items as $item) { print("$item"); $next = $items->hasNext(); if ($next) { print(", "); } }
K-gun
source share