2

Student of PHP Even Now ! Searched for this but haven't got any answer So posting the Q

i know about

sprintf ( string format [, mixed args])

sprintf Description :Returns a string produced according to the formatting string format.

and

vsprintf ( string format, array args)

Though these are pretty good enough i just ran through a problem

is there any simple way to (i mean a pretty good than iteration and sprintf each)
"Returns an array produced according to the formatting array format. " ( Desc:copied from sprintf )

i have a 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: i don't want to make a product class though! :)

2
  • No, there's no such thing. $product['p_id'] = 'Tomato' and the like for the rest, or just define the array that way to begin with $product = array('p_id'] => 'Tomato', etc....).
    – Marc B
    Commented Aug 7, 2013 at 16:44
  • @Nifty Waiting for some crazy one to Answer this , winked through goooogling ... Commented Aug 7, 2013 at 17:00

1 Answer 1

1

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

So in your 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'));
1
  • +1 thanks for your finding , i am looking for a better bro of sprintf . the given scenario in my Q is much simpler any way Commented Aug 7, 2013 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.