note 71655 deleted from function.printf by nlopess

From: Date: Sun, 10 Dec 2006 11:21:12 +0000
Subject: note 71655 deleted from function.printf by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-120477@lists.php.net to get a copy of this message
Note Submitter: soylent at soylentgreens dot com ---- 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?

« previous php.notes (#120477) next »