PHP : array in Format part to sprintf or vprintf -


student of php ! searched haven't got answer posting q

i know

sprintf ( string format [, mixed args]) 

sprintf description :returns string produced according formatting string format.

and

vsprintf ( string format, array args) 

though these pretty enough ran through problem

is there simple way (i mean pretty iteration , sprintf each)
"returns array produced according formatting array format. " ( desc:copied sprintf )

i have general $product array

$product = array(             "p_id" => '%s',             "u_price" => '%s',             "qty" => '%d'           );     $newproductarray1= sprintf_special($product,"tomato","30","12");    $newproductarray2= sprintf_special($product,"carrot","20","10"); 

so that

 $newproductarray1= (                 "p_id" => 'tomato',                 "u_price" => '30',                 "qty" => '12'               )  $newproductarray2= (                 "p_id" => 'carrot',                 "u_price" => '20',                 "qty" => '10'               ) 

nb: don't want make product class though! :)

sounds you're after array_combine, see http://www.php.net/manual/en/function.array-combine.php.

so in case:

$product_keys = array(             "p_id",             "u_price",             "qty"           );  $newproductarray1= array_combine($product_keys, array("tomato","30","12")); $newproductarray2= array_combine($product_keys, array('carrot', '20', '10')); 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -