note 71655 added to function.printf
| From: | soylentatsoylentgreensdotcom at osu1 dot php dot net | Date: | Sun, 10 Dec 2006 09:50:05 +0000 |
| Subject: | note 71655 added to function.printf | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-120472@lists.php.net to get a copy of this message | ||
I wanted to achieve (something like) this:
<?
$format = "A %s %s %s.\n";
$array1 = Array("monkey", "cow", "rooster");
$array2 = Array("eats", "goes", "crows");
$array3 = Array("bananas", "moo", "in the morning");
printf($format, $array1, $array2, $array3);
?>
Output:
A monkey eats bananas.
A cow goes moo.
A rooster crows in the morning.
but I couldn't find any php function to put in for printf that would work (vprintf comes
close). So I created this little function (and used it to create a select box):
<?
/*
printf_arrays( string format, [array args[, array ...]] )
*/
function printf_arrays($format) {
$args = func_get_args();
array_shift($args); // get rid of format
for($i=0; $i<count($args[0]); $i++) {
$pfargs = Array();
foreach($args as $arr) $pfargs[] = (is_array($arr) && $arr[$i]) ? $arr[$i] : '';
vprintf($format, $pfargs);
}
}
$months = Array(
'01'=>'Jan',
'02'=>'Feb',
/* etc. */
);
?>
<select name="month">
<? printf_arrays('<option value="%s">%s</option>',
array_keys($months), array_values($months)) ?>
</select>
Anyone else have any better ideas? Is there a built-in php function I missed that does this
already?
----
Server IP: 216.194.113.175
Probable Submitter: 66.140.222.27
----
Manual Page -- http://www.php.net/manual/en/function.printf.php
Edit -- https://master.php.net/note/edit/71655
Del: integrated -- https://master.php.net/note/delete/71655/integrated
Del: useless -- https://master.php.net/note/delete/71655/useless
Del: bad code -- https://master.php.net/note/delete/71655/bad+code
Del: spam -- https://master.php.net/note/delete/71655/spam
Del: non-english -- https://master.php.net/note/delete/71655/non-english
Del: in docs -- https://master.php.net/note/delete/71655/in+docs
Del: other reasons-- https://master.php.net/note/delete/71655
Reject -- https://master.php.net/note/reject/71655
Search -- https://master.php.net/manage/user-notes.php