问题
i have this $_categories as array()
<?php print_r($_categories); ?>
is this: Array ( [0] => 13 [1] => 7 )
what i need is to extract de values 13 and 7 into this format: 13,7 (without comma after the last value).
i have this code but is not there yet... the result is: 137 and not 13,7
<?php
if ( is_array($_categories) ) {
foreach ($_categories as $key => $value) {
$out = array();
array_push($out, $value);
echo implode(', ', $out);
}
}
else {
echo '<li>There are no saved values yet.</li>';
}
?>
Thanks, nelson
回答1:
Directly use
echo implode(', ', $_categories);
回答2:
Everytime you are implode
ing just one element,and echo
ing just it. Try like this:
$out = array(); //putting outside of the loop
foreach ($_categories as $key => $value) {
array_push($out, $value);
}
echo implode(', ', $out); //putting outside of the loop
来源:https://stackoverflow.com/questions/11671733/how-to-separate-array-with-commas